Wright State University
Department of Computer Science and Engineering
CS 480/680 Comparative Languages
Summer 2012
Assignment
1
Prasad
Recursive
Definitions (15
pts) (Due: July 18)
1.
Write a function init, which takes a list of
symbols L, and produces a list of all (top-level) symbols of L except the last
one. The input must be a non-empty list (precondition). E.g.,
(init '(a b c d)) = (a b c)
2. Write a function duplicate, which takes a nested
list of symbols L, and produces a nested list of symbols by "immediately
duplicating"
each symbol at all levels. E.g.,
(duplicate '(a b c)) = (a a b b c c)
(duplicate '( (a) b ((c)) ) = ( (a a) b b ((c c)) )
3.
Write a function lastLess, which
takes a nested list of symbols L and a symbol S, and produces another
nested list which resembles L except for the elimination of the last
occurrence of the symbol S.
E.g., (lastLess '(a b a (c e) d) 'e) = (a b a (c) d)
(Hint: Define a membership test function for nested lists by generalizing built-in member that can be used to set up lastLess recursive calls.)
4. Write a function matmul,
which takes two nested lists of numbers representing rectangular
matrices, and produces their product (or reports their
incompatibility).
E.g., (matmul '((1 2)(3 4)) '((5 6)(7 8)) = ((19 22)(43 50))
(matmul '((1 2 3)(4 5 6)) '((7 8)(9 10)(11 12)) = ((58 64)(139 154))
(Hint: Define helper functions such as transpose and dotproduct using map.)
5.
Foldr function is defined in the "Intermediate Student"-language.
Experiment with the following examples and their variants to understand
its behavior.
(foldr + 0 (list -1 5 -3 4 2))
(foldr - 0 (list -1 5 -3 4 2))
(foldr cons empty (list -1 5 -3 4 2))
(foldr append empty (list (list 1 2) (list 4) empty (list 8 1 2)))
Now define the higher-order function foldr in Scheme that satisfies the
following template and test it at least on the R5RS-Scheme-compatible
variants of the above examples (obtained by replacing empty with '() ):
(foldr f base '(x1 x2 … xn)) = (f 'x1 (f 'x2 … (f 'xn base)))
Sample "Well-documented" Definition
The
following example is for Intermediate-Student language of DrRacket. The
equivalent renditions for R5RS (Standard Scheme) are shown in asg1.ppt.
; HtDP book Exercise 5.1.2
; Develop the function chkgss
. It consumes two numbers,
guess
and
target
.
; Depending on how guess
relates to target
, the
function produces one of the following three answers:
;'TooSmall
,
'Perfect
, or
'TooLarge
.
; CONTRACT: chkgss
: number number -> { TooSmall, Perfect,
TooLarge }
; PURPOSE: chkgss
takes two numbers - a guess and a target, and
; returns a symbol TooSmall, Perfect or TooLarge
; depending on whether target is more, equal or less than guess
; CODE:
(define (chkgss
guess target)
(cond [(> guess target) 'TooLarge ]
[(< guess target) 'TooSmall ]
[(= guess target) 'Perfect ]
)
)
; TEST CASES and EXPECTED OUTCOMES
;(in general, focus on boundary conditions and coverage)
(check-expect (chkgss
3 5) 'TooSmall)
(check-expect
(chkgss
5 5) 'Perfect)
(check-expect
(chkgss
7 5) 'TooLarge)
What to hand in?
Turn-in your well-documented solution
as a Scheme (Racket) file egs.scm (egs.scm)
by running the following command on
unixapps1.wright.edu.
/common/public/tkprasad/cs480/turnin-pa1 egs.scm