2

私はLispの初心者で、二項係数の反復(階乗)を計算するが再帰的ではないプログラムをLispでプログラムしようとしています。グローバル関数、ローカル関数 (階乗)) を試してみましたが、プログラムが機能しません。たとえば、コマンド (binom (7 4)) を実行すると、エラーが発生しました。

    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)))))

もう 1 つ質問があります。emacs でコンパイルする方法を教えてください。

(バッファで試しました -> scatch -> (「binom-coeff.el」をロードしますが、エラーメッセージしかありません...)

どうもありがとう、 :)

4

3 に答える 3

3

Common Lisp で学習/プログラミングするか、emacs-lisp でプログラミングするかを決める必要があります。それらは似ていますが異なっており、学習時には混乱が障害になる場合があります。

Emacs Lisp を学ぶには:

Emacs Lisp でのプログラミング入門 http://www.gnu.org/software/emacs/emacs-lisp-intro/ または emacs M-: (info "(eintr)Top") RET と入力します。

Common Lisp について学ぶには、http://cliki.net/Getting+Startedをご覧ください。

于 2012-06-08T17:17:42.953 に答える
1

私はこのチュートリアルが好きですhttp://steve-yegge.blogspot.com/2008/01/emergency-elisp.htmlそれは本当に短くて有益です。

必要なものがない場合は、括弧を閉じてからCxCeを使用してください。そこにはかなりの数のエラーがありました。

(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
于 2012-06-12T20:39:17.843 に答える
1

あなたの最善の策は、EMACS で SLIME をインストールすることです。Common Lisp のバージョンである SBCL を使用します。CC CC または CC CK を試してコンパイルしてください。次に、CC CZ で新しいバッファを開き、プログラムを実行します。私も自分自身を教えようとしています。新しい言語を学びながら EMACS を学ぶことは、最も簡単なことではありません。少なくとも私にとっては。

于 2012-06-11T03:21:06.447 に答える