Programación Funcional - Ejercicio muy corto

 
Vista:

Ejercicio muy corto

Publicado por Ferreti (1 intervención) el 15/12/2009 09:21:21
Hola:
Tengo un problema con un ejercicio que tengo que hacer esta semana. Me sería de gran ayuda si alguien pudiese ayudarme

---------------------
Recall the type of natural numbers where we have asked the compiler to automatically generate
instances for some important type classes:

data Nat = Z | S Nat deriving (Eq,Ord,Show)

You are asked to do the following:

1.1 (1pt) Write the instance Enum Nat where toEnum is de_ned as a total function that
returns Z for negative integers. Some examples:
*Practica1> toEnum (-1) :: Nat
Z
*Practica1> toEnum 0 :: Nat
Z
*Practica1> toEnum 1 :: Nat
S Z
*Practica1> fromEnum (S (S Z))
2

The type annotation `:: Nat' is needed to help the interpreter resolve toEnum's overloading.
----------------

Yo intento algo asi:

data Nat = Z | S Nat deriving (Eq,Ord,Show)

instance Enum Nat where
toEnum n = if n<1 then
Z
else
S(toEnum (n-1))

compilar compila pero cuando lo pruebo el interprete me dice:

Main> toEnum -1
ERROR - Unresolved overloading
*** Type : (Enum a, Num (Int -> a)) => Int -> a
*** Expression : toEnum - 1

Main> toEnum (-1)
ERROR - Unresolved overloading
*** Type : Enum a => a
*** Expression : toEnum (-1)

tal vez sea simplemente definir las dos funciones, sobre cargando para el Nat. pero no se si eso se considera instanciar, a lo mejor no es lo que me piden

toEnum :: Nat ->Int
.
.
.

fromEnum :: Int -> Nat
.
.
.

p.d: y nose a que se refieren con la parte de “The type annotation `:: Nat' is needed to help the interpreter resolve toEnum's overloading.”

muchas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder