what do I have to do exactlry with this exercises

Hello, Im self studing Haskell with the Craft o ffunctional programmimg of Hutton. Now I see two exercises that I do not understand what is really the purpose here. The two exercises are : 4.21 Given a function f of type Integer -> Integer give a recursive definition of a function of type Integer -> Integer which on input n returns the maximum of the values f 0, f 1, ..., f n. You might find the max function defined in Section 3.4 useful. To test this function, add to your script a definition of some values of f thus: f 0 = 0 f 1 = 44 f 2 = 17 f _ = 0 and so on; then test your function at various values. 4.22 Given a function f of type Integer -> Integer give a recursive definition of a function of type Integer -> Bool which on input n returns True if one or more of the values f 0, f 1, ..., f n is zero and False otherwise.

On Fri, Oct 30, 2015 at 8:20 AM, Roelof Wobben wrote:
Im self studing Haskell with the Craft o ffunctional programmimg of Hutton.
Now I see two exercises that I do not understand what is really the purpose here.
Maybe a slight rewording of the instructions would help? (It helped me!) The two exercises are :
4.21 Given a function f of type Integer -> Integer give a recursive definition of a function of type Integer -> Integer which on input n returns the maximum of the values f 0, f 1, ..., f n. [...]
Given: f :: Integer -> Integer Define: g :: Integer -> Integer g n = ... such that g is defined recursively. g n returns the maximum of f 0, f 1, ..., f n. 4.22 Given a function f of type Integer -> Integer give a recursive
definition of a function of type Integer -> Bool which on input n returns True if one or more of the values f 0, f 1, ..., f n is zero and False otherwise.
Try a similar rewording as above. Regards, Sean

On Fri, Oct 30, 2015 at 8:40 AM, Roelof Wobben wrote:
Op 30-10-2015 om 07:35 schreef Sean Leather:
On Fri, Oct 30, 2015 at 8:20 AM, Roelof Wobben wrote:
Im self studing Haskell with the Craft o ffunctional programmimg of Hutton.
Now I see two exercises that I do not understand what is really the purpose here.
Maybe a slight rewording of the instructions would help? (It helped me!)
The two exercises are :
4.21 Given a function f of type Integer -> Integer give a recursive definition of a function of type Integer -> Integer which on input n returns the maximum of the values f 0, f 1, ..., f n. [...]
Given:
f :: Integer -> Integer
Define:
g :: Integer -> Integer g n = ...
such that g is defined recursively. g n returns the maximum of f 0, f 1, ..., f n.
Thanks,
But is the maxium not always the last answer.
Indeed, it is not. So the max of g 1 = answer g1 ??
The value of g 0 is f 0. The value of g 1 is either f 0 or f 1, whichever is greater. The value of g 2 is one of f 0, f 1, or f 2, whichever is greatest. And so on. Regards, Sean

On Fri, Oct 30, 2015 at 8:52 AM, Roelof Wobben wrote:
Let's say f is a recursive function which calculates the fac.
So f 0 = 0 f1 = 1 f2 = 2 f3 = 6
so im my oponion g1 = the answer of f1 which is also the max
But recall the problem definition: 4.21 Given a function f of type Integer -> Integer give a recursive
definition of a function of type Integer -> Integer which on input n returns the maximum of the values f 0, f 1, ..., f n.
It doesn't say anything about f, which means you are not allowed to make assumptions about the definition of f. f could be defined as: f :: Integer -> Integer f 42 = 100 f x = 0 Since you don't know anything about f, you must look at all values from f 0 to f n in order to find the maximum. Regards, Sean

On Fri, Oct 30, 2015 at 9:05 AM, Roelof Wobben wrote:
Now I have to think about how to read the values of f 1 .. fn 2 . I think I need 2 recursive function . One for g x and one for reading f1 or use a list to store f 0 .. f n and then use maximum.
There are different ways to solve the problem, certainly. I think this problem is asking you to do the recursion yourself rather than use existing recursive functions such as 'maximum' from the Prelude. I would assume you are allowed to define other functions to help you as needed. Regards, Sean

On 30/10/2015, at 8:05 pm, Roelof Wobben
Now I have to think about how to read the values of f 1 .. fn 2 .
You "read the value of f 1" by calling f 1.
I think I need 2 recursive function . One for g x and one for reading f1
NO. You were told to write *A* recursive function. That's ONE recursive function. That's all you need. g 0 = the maximum of [f 0] = what? g n = the maximum of [f 0, f 1, ..., f (n-1), f n] Now, DO YOU KNOW A FUNCTION THAT YOU CAN CALL TO COMPUTE the maximum of [f 0, f 1, ..., f (n-1)] ? Can you see HOW TO COMBINE that answer with f n to get the final result you want? Is there a function MENTIONED IN THE PROBLEM that could do this last step?

On 30/10/2015, at 7:52 pm, Roelof Wobben
Let's say f is a recursive function which calculates the fac.
So f 0 = 0 f1 = 1 f2 = 2 f3 = 6
so im my oponion g1 = the answer of f1 which is also the max
True. But what if f is *NOT* the factorial?
To quote your own original message,
<quote>
To test this function, add to your script a definition of some values of f thus:
f 0 = 0
f 1 = 44
f 2 = 17
f _ = 0
and so on; then test your function at various values.
</quote>
This f is NOT the factorial function. For this f,
we expect g n = if n == 0 then 0 else 44, so that
(g n == f n) is false almost always.
Let's consider the general pattern for a primitive recursive
function on the natural numbers:
g 0 otherArgs = b otherArgs
g (n+1) otherArgs = c n (g n otherArgs) otherArgs
where b(ase) and c(ombination) are primitive recursive.
In this case, there are no otherArgs, so
g 0 = <<some expression possibly involving f>>
g n = <

On 30/10/2015, at 7:20 pm, Roelof Wobben
Hello,
Im self studing Haskell with the Craft o ffunctional programmimg of Hutton.
Now I see two exercises that I do not understand what is really the purpose here.
The two exercises are :
4.21 Given a function f of type Integer -> Integer give a recursive definition of a function of type Integer -> Integer which on input n returns the maximum of the values f 0, f 1, ..., f n. You might find the max function defined in Section 3.4 useful. To test this function, add to your script a definition of some values of f thus: f 0 = 0 f 1 = 44 f 2 = 17 f _ = 0 and so on; then test your function at various values.
You are to write a function of type Integer -> Integer. So it doesn't receive f as a parameter, f is "given" by being visible. g :: Integer -> Integer g n = the maximum of [f 0, ..., f n] In general you would have two ways to do this: (1) Roll your own recursion: g 0 = the maximum of [f 0] = ??? g n = the maximum of [f 0, ..., f (n-1), f n] = some combination of the maximum of [f 0 ... f (n-1)] and f n This is a simple 2-line recursion. (2) Actually make the list [f 0, ..., f n] -- there are several ways to do that, a list comprehension is readable but `map' could be used -- and find the maximum from the list (so you are looking for a built in function Ord t => [t] -> t) This is a one-liner, but it's not recursive (or rather, YOUR part of it is not recursive), so this alternative is ruled out. The purpose here is to get you writing a simple recursion.
4.22 Given a function f of type Integer -> Integer give a recursive definition of a function of type Integer -> Bool which on input n returns True if one or more of the values f 0, f 1, ..., f n is zero and False otherwise.
The purpose here is probably to get you to realise that 4.21 and 4.22 have essentially the same solution (or more precisely, have solutions which are structurally the same).
participants (3)
-
Richard A. O'Keefe
-
Roelof Wobben
-
Sean Leather