2016-06-10 37 views
1

我看到一本關於如何創建方案的地圖功能的代碼,代碼如下:如何調用該地圖功能

(define map (lambda (f L) 
       (if null? L '() 
        (cons (f (car L)) (map f (cdr L)))))) 

(define square (lambda (x) 
    (* x x)))   

(define square-list (lambda (L) 
         (map square L))) 

按說我可以稱它爲

(map square-list '(1 2 3 4)) 

但它扔我下面的錯誤:

SchemeError: too many operands in form: (null? L (quote()) (cons (f (car L)) (map f (cdr L)))) 

Current Eval Stack: 
------------------------- 
0: (map square-list (quote (1 2 3 4))) 

我應該如何調用該函數?

回答

3

你有兩個錯誤。首先,你忘了環繞null?檢查用括號(並注意一個更好的方式來縮進代碼):

(define map 
    (lambda (f L) 
    (if (null? L) 
     '() 
     (cons (f (car L)) 
       (map f (cdr L)))))) 

其次,你預計稱這樣的過程:

(square-list '(1 2 3 4 5)) 
=> '(1 4 9 16 25) 
0

你錯過括號左右null? L,即你的條件或許應該像

(if (null? L) '() 
    (cons ...))