Course 287 Lecture 5: Functions on Lists, Higher Order Functions


The append  function joins two lists together to make one.
The member?   function asks whether an object belongs to a list
The construct (and expr-1....expr-n) is a special form.
The construct (or expr-1...expr-n) is a special form. The cond  special-form is more convenient than nested if's
The select_set  function lets you choose members out of a list
The trace  function allows you to see what a program is doing.
The example  function allows you run tests on functions as you compile
The list  function is more convenient than cons  for building longer lists.
lambda   expressions which take a variable number of arguments
Some examples of the use of map_list
The reduce  function further generalises map_list .

In this lecture we learn about the special forms cond , trace . We learn also about the useful built-in functions append , member map , example  and list . We also write our own version of member, member?  . These built-in functions will help us implement algorithms on finite sets.

The append  function joins two lists together to make one.

The append  function is built into Scheme.

It produces a new list

This starts with the elements of list1 

and finishes with those of list2 .

(define (append list1 list2)
        (if (null? list1) list2
            (cons (car list1) (append (cdr list1) list2))))

(append '(1 2 3) '(4 5 6))

==> (1 2 3 4 5 6)


(cons '(1 2 3) '(4 5 6))

    ==>  ((1 2 3) 4 5 6)

(append '((1 2) 3) '(4 (5 6)))


==> ((1 2) 3 4 (5 6))


The member?   function asks whether an object belongs to a list

(define (member? x list)
     (if (null? list) #f                                ;(1)
         (if (equal? x (car list)) #t                   ;(2)
              (member? x (cdr list)))))                 ;(3)



(member x list) evaluates to #f if x is not a member of list,

otherwise it evaluates to the that part of list which begins with the first occurrence of x.

The special forms and and or