[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Assignments] [Labs]
Back to Untyped Functional Programming in Scheme. On to Continuations.
Held Friday, February 5
Summary
Contents
Notes
reverse. Did anyone come up with one?
;;; Square a value
(define (square x) (* x x))
;;; Square all the elements of a list
(define (squareall l)
(if (null? l) nil
(cons (square (car l)) (squareall (cdr l)))))
;;; Add one to a value
(define (increment x) (+ x 1))
;;; Increment all the elements of a list
(define (incrementall l)
(if (null? l) nil
(cons (increment (car l)) (incrementall (cdr l)))))
;;; Apply a function to each element of a list
(define (applyall fun l)
(if (null? l) nil
(cons (fun (car l)) (applyall fun (cdr l)))))
map is the most common. It does not have a specified
order. It can also work on multiple lists. Read
the Scheme report for more information.
(lambda (x) b).
(map (lambda (x) (+ x 2)) l)
(define (compose f g) (lambda (x) (f (g x))))
(define (fun args) body) (define fun (lambda (args) body))
You are not required to turn in anything for the lab. However, you are expected to consider as many of these questions as you can. You are encouraged to work with other students. Those who survived the Scheme-based introduction are encouraged to act as TAs.
Use map to square all the elements of some sequence.
Use map to increment all the elements of some sequence.
Write a function, makepairs that, given two lists, makes the
list of pairs of elements from those two lists. For example
(makepairs '(1 2 3) '(a b c)) should create the list
((1 a) (2 b) (3 c)). You should write makepairs
recursive. Do not use map to write this function.
Rewrite makepairs without recursive calls, using
map.
Write a function, mappairs, that applies a binary function
to each pair in a list of pairs, creating a list of results. jor example,
(mappairs * '((1 2) (2 3) (3 4))) should give
(2 6 12).
Write a function, sum, that sums the elements in a list.
For example, (sum '(1 2 3)) should give 6.
Using all of these functions, write innerProduct that
computes the inner product of two vectors.
If you still have time, start work on assignment two.
History
Back to Untyped Functional Programming in Scheme. On to Continuations.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Assignments] [Labs]
Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS302/99S/Outlines/outline.06.html
Source text last modified Fri Feb 5 13:03:36 1999.
This page generated on Fri Feb 5 13:05:35 1999 by SiteWeaver. Validate this page.
Contact our webmaster at rebelsky@math.grin.edu