Try
(define (makeListPickerFor l)
(lambda (n)
(list-ref l n)))
so makeListPickerFor
returns a function that still has access to the initial list l
through a closure. Then
> (define S (makeListPickerFor '(4 0 -2 -3 5)))
> (S 4)
5
> (S 2)
-2
Note that in Scheme lists are 0-indexed hence the results. If you want to use 1-indexed, change to
(define (makeListPickerFor l)
(lambda (n)
(list-ref l (- n 1))))
> (define S (makeListPickerFor '(4 0 -2 -3 5)))
> (S 4)
-3
> (S 2)
0
The procedure could also be expressed as
(define makeListPickerFor
(lambda (l)
(lambda (n)
(list-ref l (- n 1)))))
this is exactly the same as the previous procedure, since (define (f x))
is syntactic sugar for (define f (lambda (x)))
, but makes it clearer that it is a procedure returning a procedure.