0

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))
Sara
  • 3
  • 2

2 Answers2

0

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
Óscar López
  • 232,561
  • 37
  • 312
  • 386
-1

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.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Thanks for the tip with length? I didn't want to use the built-in length function, so that's why there is a ?. I'll keep that fact in mind. – Sara Nov 22 '16 at 20:03