SCHEME Mutable Functions

别来无恙 提交于 2019-12-12 03:52:51

问题


I've been self-teaching myself Scheme R5RS for the past few months and have just started learning about mutable functions. I've did a couple of functions like this, but seem to find my mistake for this one.

(define (lst-functions)
  (let ((lst '()))
    (define (sum lst)     
      (cond ((null? lst) 0)
            (else
             (+ (car lst) (sum (cdr lst))))))
    (define (length? lst)
      (cond ((null? lst) 0)
            (else
             (+ 1 (length? (cdr lst))))))
    (define (average)
      (/ (sum lst) (length? lst)))
    (define (insert x)
      (set! lst (cons x lst)))
    (lambda (function)
      (cond ((eq? function 'sum) sum)
            ((eq? function 'length) length?)
            ((eq? function 'average) average)
            ((eq? function 'insert) insert)
            (else
             'undefined)))))

(define func (lst-functions))
((func 'insert) 2)
((func 'average))

回答1:


You're not declaring the lst parameter in the procedures that use it, but you're passing it when invoking them. I marked the lines that were modified, try this:

(define (lst-functions)
  (let ((lst '()))
    (define (sum lst)     ; modified
      (cond ((null? lst) 0)
            (else
             (+ (car lst) (sum (cdr lst))))))
    (define (length? lst) ; modified
      (cond ((null? lst) 0)
            (else
             (+ 1 (length? (cdr lst))))))
    (define (average)
      (/ (sum lst) (length? lst)))
    (define (insert x)
      (set! lst (cons x lst)))
    (lambda (function)
      (cond ((eq? function 'sum) (lambda () (sum lst)))        ; modified
            ((eq? function 'length) (lambda () (length? lst))) ; modified
            ((eq? function 'average) average)
            ((eq? function 'insert) insert)
            (else
             'undefined)))))

Now it works as expected:

(define func (lst-functions))
((func 'insert) 2)

((func 'average)) 
=> 2
((func 'sum))
=> 2
((func 'length))
=> 1



回答2:


Some of your functions are recursive but defined without argument. Thus (sum (cdr lst)) shouldn't work since sum uses lst. You could do it by defining a helper:

(define (sum-rec lst)
  (if (null? lst)
      0
      (+ (car lst) (sum-rec (cdr lst)))))

Or perhaps with an accumulator:

(define (sum-iter lst acc)
  (if (null? lst)
      acc
      (sum-iter (cdr lst) (+ (car lst) acc)))

Your sum would of course use it passing the lst:

(define (sum)
  (sum-iter lst 0))

Or you can just have the driver partial apply them like this:

(lambda (function)
      (cond ((eq? function 'sum) (lambda () (sum-iter lst))
            ...))

A side note. length? is a strangely named function. A question mark in the end of a name is usually reserved for functions that return a true or a false value and this clearly returns a number.



来源:https://stackoverflow.com/questions/40749948/scheme-mutable-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!