Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme how to create a list

Tags:

list

scheme

Okay this may sound like a ridiculous question, but how do you return a list in scheme.?

like image 869
poorStudent Avatar asked Mar 30 '10 00:03

poorStudent


People also ask

How is a list in Scheme?

Scheme provides a handy procedure that creates proper lists, called list . list can take any number of arguments, and constructs a proper list with those elements in that order. You don't have to remember to supply the empty list--- list automatically terminates the list that way.

How do you declare an empty list in Scheme?

The Empty List (Hunk E) In Scheme, there is one null pointer value, called "the empty list," which prints as () . (Later, we'll see why it's written that way, and why it's called "the empty list.") Conceptually, the empty list is a special object, and a "null" pointer is a pointer to this special end-of-list object.

What does cons in Scheme do?

In Scheme, car , cdr , and cons are the most important functions. The cons function is used to construct pairs and pairs are used to construct the lists. The car and cdr are used to access data and return accordingly first and second element from a pair.


2 Answers

Based on seeing some of your other questions, I think you may be having trouble getting your head wrapped around the concepts central to a functional language such as Scheme.

At the level you're learning Scheme (novice), every function you write has an input and an output, and the body of every function is a single expression. Whatever value that expression evaluates to is returned by the function. There is no need to explicitly "return" anything as you would in an imperative language like Java or C; it just happens as a direct consequence of evaluating the expression.

The body of a function is a single expression. It's not like Java where the body of a method consists of a series of instructions:

do this
then do that
then do something else
then return something (maybe)

Scheme functions evaluate a single expression; nothing more. Here's a simple function that adds 5 to whatever number is passed as an argument:

(define (add5 x)
  (+ x 5))

The body of the function is (+ x 5), which is just an expression to be evaluated. The value of x is plugged in, the + (addition) function is applied to x and 5, and the result is returned.

Lists aren't much different. All you need is an expression that will construct a list. Two have already been mentioned: list is used to build a list from scratch if you already have all the elements; cons is used to add a single element to an existing list and is often used recursively.

Here's a function that consumes a number n and builds the list (n n-1 n-2 ... 0)

(define (makelist n)
  (if (= n 0)
     (list 0)                       ; base case. Just return (0)
     (cons n (makelist (- n 1)))))  ; recursive case. Add n to the head of (n-1 n-2 ... 0)

In both the base and recursive cases, a list is returned by simply evaluating an expression that uses one of the list-building functions.

Here's another example. This one uses our add5 function to add 5 to each element of a list of numbers (lon):

(define (add5list lon)
  (if (null? lon)
    `()                 ; base case: lon is empty. Return an empty list.
    (cons (add5 (car lon)) (add5list (cdr lon)))))  ; recursive case.
                                                    ; Add 5 to the head of lon and prepend it to the tail of lon

Again, both the base and recursive cases are returning lists by evaluating expressions that result in lists.

The key thing to remember about Scheme is all functions return something, and that something is simply the result of evaluating an expression. The body of a Scheme function is a single expression.

like image 86
Barry Brown Avatar answered Sep 20 '22 13:09

Barry Brown


You probably want simply: '(2 3 5 7 11) or (list 2 3 5 7 11)?

You can also construct lists by specifying an element and a list to add it to: (cons 2 (cons 3 '()))

Here's an example of returning a list from a function:

(define returnlist 
  (lambda(a b c) 
    (cons a (cons b (cons c '())))
))

(returnlist 2 3 4)

Return value will be the list: (list 2 3 4)

like image 28
Brian R. Bondy Avatar answered Sep 19 '22 13:09

Brian R. Bondy