?
Solved

Passing a list as a Parameter in Scheme

Posted on 2013-11-02
3
Medium Priority
?
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes
Suggested Courses

752 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