【问题标题】:List of procedures in scheme计划中的程序清单
【发布时间】:2016-02-19 10:54:11
【问题描述】:

在方案中,我希望能够有一个程序列表,我可以通过地图在数字列表中使用这些程序。

比如说我有程序

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

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

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

但这会产生一个错误,称为 (foo '(1 2 11 12) '(overten?)) ?

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

错误是

The object overten? is not applicable.

【问题讨论】:

    标签: scheme


    【解决方案1】:

    因为'(overten?) 是一个包含符号的列表。仅当您评估了overten? 时,您才能返回该过程。您需要编写 (list overten?) 以便评估 list 的参数(与 quote 不同)。

    Why does Scheme have both list and quote?

    【讨论】:

      【解决方案2】:

      '(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? 不比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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-13
        • 1970-01-01
        • 1970-01-01
        • 2010-12-29
        • 2019-03-29
        相关资源
        最近更新 更多