On this page:
compose-func
flatten
flatten-foldr
bucket
tree-map
add-last-name

3.3 Higher-order Functions

Develop the following functions and datatypes in the Intermediate Student with Lambda language.

Rewrite check-temps1, check-temps, convert, average-price, convertFC, and eliminate-exp from the Lists and Trees assignment using map, filter, foldl, or foldr.

procedure

(compose-func after before)  (alpha . -> . gamma)

  after : (beta . -> . gamma)
  before : (alpha . -> . beta)
Returns the composition of before and after.

Obviously, you shouldn’t use compose, because the point is for you to understand how such things work.

procedure

(flatten lolon)  (listof number?)

  lolon : (listof (listof number?))
Produces a list of all the numbers in the elements of lolon.

Don’t use foldr.

For example, (flatten (list (list 1 2) (list 3 4 5) (list 6))) returns (list 1 2 3 4 5 6).

procedure

(flatten-foldr lolon)  (listof number?)

  lolon : (listof (listof number?))
Produces a list of all the numbers in the elements of lolon.

Use foldr.

procedure

(bucket lon)  (listof (listof number?))

  lon : (listof number?)
Returns a list of sublists of adjacent equal numbers.

Use foldr.

For example, (bucket (list 1 1 2 2 2 3 1 1 1 2 3 3)) returns (list (list 1 1) (list 2 2 2) (list 3) (list 1 1 1) (list 2) (list 3 3)).

procedure

(tree-map f tree)  (or/c unknown? person?)

  f : (string? . -> . string?)
  tree : (or/c unknown? person?)
Returns a tree where f has been applied to every person’s name in tree.

procedure

(add-last-name tree lname)  (or/c unknown? person?)

  tree : (or/c unknown? person?)
  lname : string?
Returns a tree where lname has been appended to every person’s name in tree.

You should use tree-map and string-append. If you do not re-use your tree-map, you will get a zero.