Solved

Passing a list as a Parameter in Scheme

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 70
Dialogbox API leak? 18 62
advertisement module in core php 4 94
move one pixel 4 42
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
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 …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

10 Experts available now in Live!

Get 1:1 Help Now