Scheme函數的功能為何?
(define (x list)
cond ( ( null ? lis ) 0)
( ( not (list ? (car lis )))
( cond (( eqv ? (car lis ) nil ) ( x ( cdr lis )))
( else ( + 1 (x ( cdr lis ))))
)
( else ( + ( x ( car lis )) ( x (cdr lis )))
)
)
解:
等於以下LISP函數
(defun x (lis)
( cond ( ( NULL lis ) 0)
( ATOM (car lis ))
( cond ( ( EQ (car lis ) Nil ) ( x ( cdr lis)))
( T ( +1 ( x (cdr lis ))))
)
( T ( + ( x ( car lis )) ( x ( cdr lis ))))
)
)
※此函數可計算一個串列中原子(atom)的個數
例如
呼叫x ( A ( B C ) ( D ( ( E F ))) =>回傳6
呼叫 x ( nil nil a nil b c ) => 回傳3
沒有留言:
張貼留言