2

i´m a newbie in lisp , i try to programm a programm in lisp, that calculate binomial coefficient iterative (factorial) but NOT recursive. i´ve try everthing, global function, local function (factorial)), but my programm doesn´t work, for example when i command: (binom (7 4)), just got an error

    SELECT ALL
(defun binom-coef(a b)   
       (if (or (< a b) (< b 0))
       nil            )    
      
       (flet fakul(n)    ; factorial
               (cond ((= n 0) 1)
              (t (* n (fakul (- n 1))))))
   (/ (fakul a) (* (fakul b) (fakul(- a b)))))

i´ve one more question, how to compile in emacs?

(i tried in buffer -> scatch -> (load "binom-coeff.el" but there´s only a error message...)

Many thanks, :)

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
milk05
  • 21
  • 2
  • Here is a good introductory Common Lisp book for download: http://www.cs.cmu.edu/~dst/LispBook/index.html Otherwise you should look for an Emacs Lisp introduction. – Rainer Joswig Jun 08 '12 at 16:23

3 Answers3

3

You must make up your mind whether you're learning/programming in Common Lisp or in emacs-lisp. They're similar but different, and when learning, confusion may be an impediment.

To learn Emacs Lisp read:

An Introduction to Programming in Emacs Lisp http://www.gnu.org/software/emacs/emacs-lisp-intro/ or type in emacs M-: (info "(eintr)Top") RET

To learn about Common Lisp, have a look at http://cliki.net/Getting+Started

1

Your best bet is to install SLIME with EMACS. It uses SBCL which is a version of common lisp. Try C-C C-C or C-C C-K to compile. Then C-C C-Z to open a new buffer and run the program. I'm trying to teach myself also. Learning EMACS while learning a new language is not the easiest thing to do. At least for me.

eaandkw2
  • 33
  • 4
1

I like this tutorial http://steve-yegge.blogspot.com/2008/01/emergency-elisp.html it's really short and informative.

If it's elisp what you want, just use C-x C-e after closing parenthesis. You had quite a number of errors there.

(defun binom-coef(a b)
  ;; (if (or (< a b) (< b 0)) nil)
  ;; Wery strange expression. (if CONDITION IF-TRUE IF-FALSE). You
  ;; didn't set IF-FALSE, so it's nil by default, 
  ;; and you set IF-TRUE to nil. It allways returns nil.

  ;; If you want to leave from function when wrong args given
  (block nil
    (if (or (< a b) (< b 0)) (return))
  ;; can be throw/catch also

    ;; (flet fakul(n)
    ;;   ;; wrong usage of flet. It's used like let, (flet ((name1 args1
    ;;   ;; body1) (name2 args2 body2) ... )
    ;;   ;; BODY-WHERE-FUNCTIONS-ARE-VISIBLE)
    ;;   (cond
    ;;     ((= n 0) 1)
    ;;     (t (* n (fakul (- n 1))))
    ;;     ))

    (flet ((fakul (n)
             (cond
               ((= n 0) 1)
               (t                       ; shound be like (< 0 n)
                 (* n (fakul (- n 1))))
               )))
      (fakul 5)
      ;; => 120
      (/ (fakul a) (* (fakul b) (fakul(- a b))))
      ;; ^ Inside flet ^
      ))
  )
(binom-coef 8 3)  ; <= look, it's not (8 3), because (8 3) means
                  ; execute function `8' with argument 3. If you
                  ; wanted to pass list with 8 and 3, it should be
                  ; (quote (8 3)), or simply '(8 3)
;; => 56
desudesudesu
  • 2,185
  • 1
  • 15
  • 20