gerrycr
asked on
Passing a list as a Parameter in Scheme
I am a beginner to learning the Scheme programming language and I want to be able to read values from a console into a list, pass the list as a parameter, and return the sum of that list.
I want to get this result: Ex (display (sum-list-members '(1 2 3 4 5))) but the user must enter these values at the console.
This is what I am working on.
(begin
(define count 0)
(define sum-list-members
(lambda (lst)
(if (null? lst)
0
(+ (car lst) (sum-list-members (cdr lst))))))
(display "Enter a integer [press -1 to quit]: ")
(newline)
(let loop ((i 0))
(define n(read))
(sum-list-members (list n))
(set! count i)
(if (not(= n -1))
(loop (+ i 1)))
)
(newline)
)
I want to get this result: Ex (display (sum-list-members '(1 2 3 4 5))) but the user must enter these values at the console.
This is what I am working on.
(begin
(define count 0)
(define sum-list-members
(lambda (lst)
(if (null? lst)
0
(+ (car lst) (sum-list-members (cdr lst))))))
(display "Enter a integer [press -1 to quit]: ")
(newline)
(let loop ((i 0))
(define n(read))
(sum-list-members (list n))
(set! count i)
(if (not(= n -1))
(loop (+ i 1)))
)
(newline)
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked. Thanks for your help.
Here's my suggstion for the sum function.
Open in new window
This at least works.
(sum-list-members '(1 2 3))
6
I don't know which Scheme you are using, but I suggest downloading DrRacket and start reading through the helf there. It's a nice Development Environment.
Regards
Friedrich