[英]Scheme Error Unknown Identifier: map when using higher order function
本質上,我正在嘗試編寫一個方案方法,它將使用 map function 對列表中的每個項目進行立方體。 所以它將 go 從 '(1 2 3 4) 到 '(1 8 27 64)。 這是我當前的代碼:
(define (cube-all lst)
(map (lambda (x) (* (* x x) x)) lst)
)
這是錯誤消息:
SchemeError: unknown identifier: map
Current Eval Stack:
-------------------------
0: map
1: (cube-all (quote (1 2 3 4)))
2: (println (cube-all (quote (1 2 3 4))))
這是由於語法不正確嗎? 或者我必須用 map 做其他事情嗎?
編輯:println 是另一個 function,它只顯示答案
如果您只能使用評論中提到的“方案”,則不能使用map
。
但是......你可以寫map
:
(define (reverse l)
(define (reverse-loop lt into)
(if (null? lt)
into
(reverse-loop (cdr lt) (cons (car lt) into))))
(reverse-loop l '()))
(define (map f l)
(define (map-loop lt into)
(if (null? lt)
(reverse into)
(map-loop (cdr lt) (cons (f (car lt)) into))))
(map-loop l '()))
(define (cube-all lst)
(map (lambda (x) (* (* x x) x)) lst))
(cube-all '(1 2 3))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.