Download Problem 1 - IDA.LiU.se

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

Mathematics of radio engineering wikipedia , lookup

Addition wikipedia , lookup

Non-standard calculus wikipedia , lookup

Dirac delta function wikipedia , lookup

Large numbers wikipedia , lookup

Collatz conjecture wikipedia , lookup

Halting problem wikipedia , lookup

Function (mathematics) wikipedia , lookup

Elementary mathematics wikipedia , lookup

History of the function concept wikipedia , lookup

Lambda calculus wikipedia , lookup

Factorial wikipedia , lookup

Transcript
TDDC74 Programming: Abstraction and Modelling
PRAM, Tutorial 2
Problem 1 ------------------------------------------------------------------------------------------------------------------This task is about naming code correctly and in a helpful fashion. Consider the following code:
(define zoo
(lambda (a b)
(= (remainder a b) 0)))
(define foo
(lambda (x y)
(if (< y 2) #t
(if (zoo x y) #f
(foo x (- y 1))))))
;; note to self: x should be an integer
(define bar
(lambda (x)
(if (< x 2) #f
(foo x (- x 1)))))
Your task is to change function and parameter names, and rewrite if to cond, for clarity. As ever,
the code should conform to conventions, and the reader should - with some thought - be able to
follow the logic of the program. This both in terms of the procedure bodies (within the lambda)
and the names themselves. A good rule of thumb in cases like this is to let the name follow the
return value, so that code calling the function becomes more readable. 1 .
You might want to do this in steps.
Hints:
• Start with zoo. What does remainder mean? What does this function return?2
• What type[s] of value do foo/bar return? Numbers? Strings? Booleans? (Does it depend on
the kind of input?)
• What type[s] of input values do foo/bar seem to take? Numbers? Strings? Booleans?
• After some initial rewriting based on the above, you might want to start considering what
the functions are actually calculating. When does foo return #f?
Discuss and compare the solution with other students, and show your assistant.
Problem 2 ------------------------------------------------------------------------------------------------------------------Write a procedure (filtered-sum fn pred from to) which calculates the sum of (fn n) from
from to to if (pred n) returns true. For example, if we want to calculate the sum of the squares
of all odd numbers between 1 and 11 inclusive3 we can do the following:
> (filtered-sum (lambda (num) (* num num)) odd? 1 11)
286
1
For instance, a scoring function might be called ”score” rather than ”calculate-the-score”. A function calling it
might contain code snippets like (if (> (score ’PlayerA gamestate) current-high-score) ...), which better
expresses ”the score of PlayerA is higher than the current high score” than (if (> (calculate-the-score ...)
...) ...) would. The function returns the score, and so the name follows this.
2
Yes, this will help you out in lab 1.
3 2
1 + 32 + 52 + 72 + 92 + 112
1
Problem 3 ------------------------------------------------------------------------------------------------------------------Write a procedure (make-safe pred fn) which creates a new procedure. pred is a predicate function which takes one argument and returns #t or #f. fn is a procedure which takes one argument.
The resulting procedure calls fn with its argument iff pred returns true for that argument. Otherwise it will give a error message by using error:
(error " Illegal argument:" arg)
Assume that you have a function factorial (which is not to be modified). With the help of
make-safe, write:
1. fac-odd, which only calculates the factorial of odd numbers
2. fac-odd-pos, which only calculates the factorial of numbers which are odd and positive.
For example:
> (fac-odd-pos 5)
120
> (fac-odd-pos 6)
Error; Illegal argument: 6
Problem 4 ------------------------------------------------------------------------------------------------------------------Write a procedure (repeated start times fn) which applies fn on itself times times, beginning
with start as the “initial” argument.
> (define inc (lambda (number) (+ number 1)))
> (repeated 42 10 inc)
52
Problem 5 ------------------------------------------------------------------------------------------------------------------Write a procedure make-repeated by which we can create procedures like repeated:
> (define add10 (make-repeated 10 inc))
> (add10 32)
42
> ((make-repeated 10 add10) 1)
101
Extra: Try to write a version that does not refer to repeated above, but is entirely self-contained.
You might want to define a function compose that takes functions f , g and returns their composition.4 Of particular interest: if f repeated one time, denoted f 1 , is such that f 1 (x) = f (x), what
should f 0 (x) be?
Hint: write down, on paper, what f 5 (x) should be, and what f 4 (x) should be. Compare.
4
A function h such that h(x) = f (g(x)), just as in Forsling & Neymark.
2