Solved

Passing a list as a Parameter in Scheme

Posted on 2013-11-02
3
429 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

749 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