Em Ter, 2005-10-25 às 17:36 -0700, [EMAIL PROTECTED] 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:[EMAIL PROTECTED]
jabber:[EMAIL PROTECTED]
UIN:50599075
MSN:[EMAIL PROTECTED]
Telefone:33346720
Celular:91844179
Endereço:Rua Paula Cândido 257/201
         Gutierrez 30430-260
         Belo Horizonte-MG
         Brasil

Attachment: signature.asc
Description: This is a digitally signed message part

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell

Reply via email to