计划中的程序清单

时间:2016-02-19 10:54:12

标签: scheme

在计划中,我希望能够通过地图获得我可以在数字列表上使用的程序列表。

例如,假设我有程序

(define (overten? x) (> x 10))

为什么在使用(foo'(1 2 11 12)'())调用时它会起作用?

(define (foo lst proc)
    (map overten? lst)
)

但这会产生一个错误,叫做(foo'(1 2 11 12)'(翻过?))?

(define (foo lst proc)
    (map (car proc) lst)
)

错误是

The object overten? is not applicable.

2 个答案:

答案 0 :(得分:2)

因为'(overten?)是包含符号的列表。只有在您评估overten?时才会收回该程序。您需要编写(list overten?)以便评估list的参数(与quote不同)。

请参阅Why does Scheme have both list and quote?

答案 1 :(得分:0)

'(overten?)不是包含程序的列表。它是一个带有符号的列表,与任何范围内与该名称绑定的程序无关

你需要考虑评估:

overten? 
; ==> {procedure overten? blabla} 
;     (a implementation dependent representation of a procedure object
'overten 
; ==> overten? 
;     (just a symbol with name "overten?", nothing to do with the procedure object above)
(list overten? 'overten?) 
; ==> ({procedure overten? blabla} overten)
      a list where the first element is a procedure and the second a symbol with name "overten?"


(define another-name-quoted 'overten?) 
; ==> undefined
; 'overten? evaluated to a symbol, them bound to another-name-quoted
(define another-name overten?)         
; ==> undefined
; overten? evaluated to a procedure, then bound to another-name

程序overten?不仅仅是overten?而是another-name。 这是我们使用过程列表的示例。它是compose程序的实现:

(define (my-compose . procs)
  (let* ((rprocs (if (zero? (length procs))
                     (list values)
                     (reverse procs)))
         (proc-init (car rprocs))
         (proc-list (cdr rprocs)))
    (lambda args
      (foldl (lambda (proc acc)
               (proc acc))
             (apply proc-init args)
             proc-list))))

(define sum-square-sub1-sqrt
  (my-compose inexact->exact
              floor
              sqrt
              sub1
              (lambda (x) (* x x))
              +))

(sum-square-sub1-sqrt 1 2 3) ; 5