| Due date: 11/14 @ 11:59pm Programming Language: Intermediate Student Language with lambda
The goal of this problem set is to practice using DrRacket's loop
functions. In addition we will look
into local
and lambda (λ) a bit more.
Problem A1:
Given the following sorting function:
;; sort-a : [Listof Number] -> [Listof Number]
;; to construct a list with all items from alon in increasing order
(define (sort-a alon)
(local ((define (insert an alon)
(cond [(empty? alon) (list an)]
[( < an (first alon)) (cons an alon)]
[else (cons (first alon)
(insert an (rest alon)))])))
(cond [(empty? alon) empty]
[else (insert (first alon) (sort-a (rest alon)))])))
Design an abstracted version of the sort-a function which consumes the comparison as an additional argument and uses a loop function.
Use this function to design sort-ascending and sort-descending , which sort a [Listof Number] in ascending and descending order, respectively.
Problem A2:
Decide which of the following phrases are legal lambda-expressions:
-
(lambda (x y) (x y y))
-
(lambda () 10)
-
(lambda (x) x)
-
(lambda (x y) x)
-
(lambda x 10)
Explain why they are legal or illegal.
Problem A3:
In an arithmetic sequence, a0, a1, a2,..., an, an+1,..., each successor term an+1 is the result of adding a fixed constant to an. For example, the sequence 8, 13, 18, 23, ... has a starting point of 3 and the constant is 5. From these two facts, called starting point and summand, respectively, all other terms in the sequence can be determined.
-
Develop the recursive function
a-fives , which consumes a natural number and recursively determines the corresponding term in the above series.
(a-fives 0) -> 8
(a-fives 1) -> 13
-
Develop the function
seq-a-fives , which consumes a natural number n and creates the sequence of the first n terms according to a-fives . Hint: Use build-list .
Problem A4:
DrRacket has lots of great abstract functions for processing lists
(pg. 313, Sect. 21.2, or the online version).
Given the following data definitions:
;; A Grade is: (make-grade Symbol Number)
(define-struct grade (letter num))
;; The Symbol in a Grade represents
;; 'A >= 90
;; 'B >= 80
;; 'C >= 70
;; 'D >= 60
;; 'F < 60
;; A [Listof Grades] ...
(define grades
(list (make-grade 'D 62) (make-grade 'C 79) (make-grade 'A 93) (make-grade 'B 84)
(make-grade 'F 57) (make-grade 'F 38) (make-grade 'A 90) (make-grade 'A 95)
(make-grade 'C 76) (make-grade 'A 90) (make-grade 'F 55) (make-grade 'C 74)
(make-grade 'A 92) (make-grade 'B 86) (make-grade 'F 43) (make-grade 'C 73)))
Design the requested functions to
manipulate Grades . You must use the given list
as one of your tests.
For each you may use a local function or
an anonymous (lambda ) function.
Note: if you do not use the DrRacket loop function
mentioned, you will not recieve credit for the sub-problem!
- Design the function
log->lon that converts
a [listof Grade] into a [Listof Number] that
contains just the numerical grade, using the Scheme
function map .
- Using
foldr , design the function best-grade
that finds the highest Grade in a [Listof
Grade] .
- Design a function
just-As that returns a list of only
the 'A grades, using filter .
- Use
andmap to design the
function all-pass? that checks to see if all the
Grades in a given list are not 'F .
- Finally design the function
bonus that adds 5
to all of the Grades in a given list, and updates the letter
portion of the Grade if it changes. Use map to
design your function... it must return a [Listof
Grade] !
|