Solved

Passing a list as a Parameter in Scheme

Posted on 2013-11-02
3
416 Views
Last Modified: 2013-11-04
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)
)
0
Comment
Question by:gerrycr
  • 2
3 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 39620849
I do not get why you enclose it all in begin, and you are not even giving the sum-list member function a parameter. You are not setting any value. And the definition of a function is wrong also. I'm wondering what you really get.

Here's my suggstion for the sum function.
(define (sum-list-members a-list)
    (if (null? a-list)
    0
    (+ (car a-list) (sum-list-members (cdr a-list)))))

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
0
 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
ID: 39620869
Sorry, my bad. you can it writ your way which the way you did. Sorry. It's not clear to me how you want to get the input from the programmer.  Do you want to read it one entry after the other?

Or should the user be able to just write

give me a list
'(1 2 3 )
or (list 1 2 3)
?

You are not checking the input but just feed it to list so what I see is you a seem to expect a number then you turn it into a a list and feed it to the counter.

So If I got that all right I came up with


(begin

  (define count 0)
  (define myList (list))

(define sum-list-members
  (lambda (a-list)
    (if (null? a-list)
    0
    (+ (car a-list) (sum-list-members (cdr a-list))))))

(display "Enter a integer [press -1 to quit]: ")
(newline)



(let loop ((i 0))

  (define n(read))
  (if (not(= n -1))
    (begin
      (set! myList (cons n myList))
      (set! count i)      
      (loop (+ i 1))
      )
   )
 
)

(display (sum-list-members myList))
(newline)
)

Open in new window


instead an it seems I got what you wanted to get:
Enter a integer [press -1 to quit]: 
1
2
3
-1
6

Open in new window

0
 

Author Comment

by:gerrycr
ID: 39622286
That worked. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now