Download CS 116 Tutorial 2 (solutions): Functional abstraction

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Big O notation wikipedia , lookup

Functional decomposition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Continuous function wikipedia , lookup

Non-standard calculus wikipedia , lookup

Dirac delta function wikipedia , lookup

Function (mathematics) wikipedia , lookup

Function of several real variables wikipedia , lookup

Lambda calculus wikipedia , lookup

History of the function concept wikipedia , lookup

Transcript
CS 116 Tutorial 2
Functional Abstraction
Reminders

Assignment 2 is due this Wednesday at
Noon
Review
Build-list
 Lambda
 Function that produces a function

Applied to every
element in the list;
think of it like map
Build-list
(build-list n f)
Size of list
;;build-list: Nat (Nat->X) -> (listof X)
Builds a list of size n from 0 to n-1, then
applies f to every element in the newly
created list.
 Produces:

(list (f 0) (f 1) … (f (n-1)))
Lambda

Single-use, nameless helper function
lambda
(define (f p1 … pn)
function body here)
(lambda (p1 … pn)
function body here)
Function that produces a function
(define
MUST use
in order to
produce a
function

(f x y …)
(lambda (a b …)
function body here))
Contract: f: _ _ … -> (_ _ … ->_)
Consumed
values of f
Produced value of f /
contract for lambda
Only job of f is to call the lambda function
 Lambda function takes care of all the work

1.
Using build-list, write a Scheme
function powers-of-two that
consumes a nonnegative integer n and
produces a list of integers that are the
powers of 2 from 2n down to 20 = 1.
◦
For example,
(powers-of-two 2) => (list 4 2 1)
2.
Using build-list and filter, write a
Scheme function factors that
consumes a number n (at least 1) and
returns a list of all positive factors of n in
increasing order.
◦
For example,
(factors 4) => (list 1 2 4)
3.
Without using explicit recursion, write a
function copies that consumes a natural
number n and returns a list containing 1
copy of 1, followed by 2 copies of 2, etc.,
up to n copies of n.You may want to use
the function flatten below.
;; flatten: (listof (listof X)) -> (listof X)
;; consumes a list of lists of type X and appends these
;; lists together to create a list of type X
(define (flatten le)
(foldr append empty le))
Write a function map-posns that consumes
a function (f) and a list of posn structures
(points) and produces a new list by
applying f to the x- and y- fields of each
posn in points.
4.
◦
For example,
(map-posns (list (make-posn 0 1) (make-posn 10
4)
(make-posn -4 -4)))
 (list -1 6 0)
5.
Write a function char-countfunction-maker that consumes a
predicate and produces a one-argument
function. The function that is produced
consumes a string and produces the
number of characters for which the
predicate evaluates to true.
◦ For example,
(char-count-function-maker charupper-case?) produces a function that
consumes a string s and produces the number of
characters in s that are uppercase.
6.
Write a function map-together that consumes a
function f and two lists lst1 and lst2 of the same
length, and returns the list:
(list (f (first lst1)(first lst2))
(f (second lst1)(second lst2))
...)
that we get if we apply f to the first elements
of
both lists, then the second elements of
both lists, and so
on.
What is the contract of map-together?
ormap is another built-in abstract function in
Scheme. It consumes a function f and a list
lst, and produces true if applying f to any
value in lst produces true. Otherwise, false is
produced.
7.
◦
For example, to determine if there are any even
numbers in (list 3 7 6 1 0 9 7 -1), you
can write
(ormap even? (list 3 7 6 1 0 9 7 -1)).
What is the contract for ormap?