This preview shows pages 1–2. Sign up to view the full content.
CSCC24 – Principles of Programming Languages
Functional Programming  Scheme
Defining Functions
From last class:
Parameter: integer n ≥ 0.
Result: sum of
integers from 0 to n.
(
define (sumn n)
(cond
)
)
What could/should we change if
____________?
(sumlist x): Given x a list of numbers, returns
the sum of all the numbers in x.
(
define (sumlist x)
(cond
How would you define sumn if n could be a
list of sexpressions and you only wanted to
sum those elements that are numbers?
Recursive Scheme Function: Length (Note:
There is a builtin length function.
)
(
define (length x)
(cond
)
)
This is called “cdrrecursion” which processes
the car of the list and then has a recursive call
on the cdr of the list.
More cdr recursion (append x y):
1) We recurse through the first list, taking it
apart and adding it onto the second list.
_____________________________________
_____.
Let's show some examples of this:
(append '(a) '(b c))
(append '(a b c) '(d (e f) g))
There are two bases cases.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Summer '10
 Cathy
 Functional Programming

Click to edit the document details