Solved

Passing a list as a Parameter in Scheme

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 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