Hi, My name is Michael. I'm new to Haskell. I'm working through The Craft of Functional Programming by Simon Thompson. I'm having problems with a few exercises from chapter 4. I will present one per post. This one is from 4.9 Given a function f of type Int -> Int give a recursive definition of a function of type Int -> Int which on input n returns the maximum values f 0, f 1, ... , f n I defined f as follows f 0 = 0 f 1 = 44 f 2 = 5 f 9 = 8 this works except when f n > n. In that case I get an "illegal instruction" error and hugs exits. I'm pretty sure this is a logic problem. Could someone point me in the right direction so I can think about this problem correctly. Here is my code maxOverf :: Int -> Int maxOverf m | f m > m = (f m) | otherwise = (maxOverf m-1) Any hints/help/flames welcome. Thanks Michael Litchard
Em Ter, 2005-10-25 às 17:36 -0700, michael@schmong.org escreveu:
Hi, My name is Michael.
Hello Micheal.
This one is from 4.9 Given a function f of type Int -> Int give a recursive definition of a function of type Int -> Int which on input n returns the maximum values f 0, f 1, ... , f n
I don't have the book, so I'm just taking from what you've written.
I defined f as follows f 0 = 0 f 1 = 44 f 2 = 5 f 9 = 8
this works except when f n > n. In that case I get an "illegal instruction" error and hugs exits. I'm pretty sure this is a logic problem. Could someone point me in the right direction so I can think about this problem correctly. Here is my code
maxOverf :: Int -> Int maxOverf m | f m > m = (f m) | otherwise = (maxOverf m-1)
This code ir returning f m when f m > m. This is not the maximum of f 0, f 1, ..., f n. You could try: maxOverf :: Int -> Int maxOverf m = maximum $ map f [0 .. m] or, if it's better for you to understand: maxOverf :: Int -> Int maxOverf 0 = f 0 maxOverf m | f m > biggest = f m | otherwise = biggest where biggest = maxOverf (m - 1) -- Abraços, marcot mailto:marcot@minaslivre.org jabber:marcott@jabber.org UIN:50599075 MSN:marcot@ufmg.br Telefone:33346720 Celular:91844179 Endereço:Rua Paula Cândido 257/201 Gutierrez 30430-260 Belo Horizonte-MG Brasil
On Tuesday 25 October 2005 08:36 pm, michael@schmong.org wrote:
Hi, My name is Michael. I'm new to Haskell. I'm working through The Craft of Functional Programming by Simon Thompson. I'm having problems with a few exercises from chapter 4. I will present one per post.
This one is from 4.9 Given a function f of type Int -> Int give a recursive definition of a function of type Int -> Int which on input n returns the maximum values f 0, f 1, ... , f n
I defined f as follows f 0 = 0 f 1 = 44 f 2 = 5 f 9 = 8
Most likely your problem is that f is only defined for input values 0, 1, 2 and 9. If you call f with any other value, you will end up with an error.
this works except when f n > n. In that case I get an "illegal instruction" error and hugs exits. I'm pretty sure this is a logic problem. Could someone point me in the right direction so I can think about this problem correctly. Here is my code
maxOverf :: Int -> Int maxOverf m
| f m > m = (f m) | otherwise = (maxOverf m-1)
Here you are calling f with successively decreasing values; that will run into areas where f is undefined for any value of m other than 1 or 2.
Any hints/help/flames welcome. Thanks
Try a total rather than a partial function for f. You can just add a default clause to the end, like f _ = 0 which will define f x to be 0 for all values of x except the ones already mentioned.
Robert Dockins (robdockins@fastmail.fm) wrote:
Try a total rather than a partial function for f. You can just add a default clause to the end, like
f _ = 0
btw, the above definition is in the text of the 4.9 exercise ;) Sincerely, Gour -- Registered Linux User | #278493 GPG Public Key | 8C44EDCD
Am Mittwoch, 26. Oktober 2005 02:36 schrieb michael@schmong.org:
In that case I get an "illegal instruction" error and hugs exits.
This is a bug in Hugs but your code is incorrect, too. You get into infinite recursion for some argument values and I suppose, Hugs doesn't handle infinite recursions well. At least, Hugs sometimes dies with a segementation fault on Linux when it actually should cancel the computation cleanly because of a stack/heap overflow caused by infinite recursion.
[...]
Best wishes, Wolfgang
participants (5)
-
Gour -
Marco Tulio Gontijo e Silva -
michael@schmong.org -
Robert Dockins -
Wolfgang Jeltsch