Scheme how to create a list

36,041

Solution 1

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.

Solution 2

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)

Solution 3

Another not-so-well known way to do this:

> ((lambda x x) 2 3 5 7 11)
(2 3 5 7 11)

that is, the "list" function itself can be defined as:

> (define list (lambda x x))
Share:
36,041

Related videos on Youtube

poorStudent
Author by

poorStudent

Updated on July 09, 2022

Comments

  • poorStudent
    poorStudent over 1 year

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

Related