CSC 153  Grinnell College  Spring, 2005 
Computer Science Fundamentals  
Laboratory Exercise  
This laboratory exercise provides experience defining and using higherorder procedures.
(define substitute (lambda (template old new) ;; precondition test (if (not (list? template)) (error 'substitute "The template must be a list")) (let kernel ((rest template) (result '())) (if (null? rest) (reverse result) ;; Reverse the final list, because the ;; recursion builds it back to front. (let ((first (car rest))) (kernel (cdr rest) (cons (if (equal? old first) new first) result))))))) (define sub (lambda (old new) (lambda (template) (substitute template old new))))
Using sub
, define and test a monthreplacer
procedure that substitutes the symbol November
for each
toplevel occurrence of the symbol month
in a given list.
Write a curried version of the expt
procedure.
Curriedexpt
should take one argument, a number
x
, and return a procedure that "remembers" x
and raises it to any specified power:
(define poweroftwo (curriedexpt 2)) > (poweroftwo 7) 128 > ((curriedexpt 10) 3) 1000 > ((curriedexpt 2) 5) 32 > ((curriedexpt 9/10) 4) 6561/10000 > (map (curriedexpt 9) '(2 3 1/2 3)) (81 729 3.0 1/729)
The reading about the insertion sort showed how a procedure could be defined that returns a list of numbers in ascending order. In that lab, an ordering predicate (e.g., <= or >=) is used to compare specific data, but all of the rest of the code is independent of the type of data and the nature of the ordering required.
Apply the idea of currying to produce a higherorder procedure generalsort that takes an ordering predicate (e.g., <= or >=) as parameter and that returns a sorting procedure based on that predicate. Thus, an alternative definition of sortnumbersascending might be:
(define sortnumbersascending (generalsort <=))
while a procedure for sorting list elements in descending order might be:
(define sortnumbersdescending (generalsort >=))
The call (compose car reverse)
returns a procedure. Describe
the effect of applying this procedure to a list.
Suppose that we have two procedures f
and g
of arity 1 that always return numbers as values. We can perform "function
addition" on them  that is, we can use them to generate a new procedure
that takes one argument and returns the sum of the results of applying
f
and g
to that argument. Define a procedure
functionadd
that implements the operation of function
addition.
> ((functionadd double /) 5) 51/5 > ((functionadd (lambda (n) (* n n)) (lambda (n) ( 128 n))) 100) 10028 > ((functionadd stringlength (lambda (str) (char>integer (stringref str 0)))) "America") 72 ;; 7 characters in "America", #\A is ASCII character 65 (define sinpluscos (functionadd sin cos)) > (sinpluscos 0) 1 (define pi 3.1415926535897932) > (sinpluscos (/ pi 4)) 1.414213562373095 (define sum (lambda (ls) (apply + ls))) > ((functionadd length sum) '(3 2 4 9)) 22
Why do we need to define a separate procedure functionadd
,
instead of simply applying the builtin addition procedure, +
?
In the last example above, for instance, would there be anything wrong with
simply writing ((+ length sum) '(3 2 4 9))
? If so, what?
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/labhigherorderproc.shtml
Henry M. Walker (walker@cs.grinnell.edu)
created April 2, 1997 by John David Stone last revised February 3, 2005 by Henry M. Walker 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 