2011-07-16 4 views
1

私はlispのemacsの中で見つけることができますletためlet*があるようfletためflet*はありません - ので、これら四つの選択肢のいずれかがfletによって定義された単一の関数で複数回使用する関数を定義するための、より慣用されていますか?この例では、add1は、add1twice内で再利用したいが、その外側では使用しない関数です。フレット相当のlet *?

オプション1

(defun add2 (x) 
    (flet ((add1 (x) (1+ x))) 
    (flet ((add1twice (x) 
       (add1 (add1 x)))) 
     (add1twice x)))) 

オプション2

(defun add2 (x) 
    (flet ((add1twice (x) 
      (flet ((add1 (x) (1+ x))) 
       (add1 (add1 x))))) 
    (add1twice x))) 

オプション3

(defun add2 (x) 
    (flet ((add1twice (x) 
      (let (add1) 
       (fset 'add1 (lambda (x) (1+ x))) 
       (add1 (add1 x))))) 
     (add1twice x))) 

オプション4

(labels ((add1 (x) (1+ x))) 
    (defun add2 (x) 
    (flet ((add1twice (x) 
       (add1 (add1 x)))) 
     (add1twice x)))) 

(。これらのすべてが、同じ結果を生成)

答えて

1

the documentation for fletであまり明確ではないが、そのマクロ展開は、それが許容することを明らかにし、順次結合:

(defun add2 (x) 
    (flet ((add1 (x) (1+ x)) 
     (add1twice (x) 
      (add1 (add1 x)))) 
    (add1twice x))) 

ドキュメントは「letスタイルのバインディング」— を確立言及それにもかかわらず、上記の定義は有効です。let*

+0

OOPS!最初に試してみたはずです。 :) – hatmatrix

1

またlabelsfletであり、let*letです。