How can I define a list whose elements are functions? What would be the type to define if I have a data type?:
data Usuario = UnUsuario String [**--que tipo pondria aca--**] deriving Show
How can I define a list whose elements are functions? What would be the type to define if I have a data type?:
data Usuario = UnUsuario String [**--que tipo pondria aca--**] deriving Show
The type for a simple function would be a -> b
, with a
as the data type of the domain of the function, and b
as the data type of the image. Your definition would look like this:
data Usuario a b = Usuario String [a -> b] deriving Show
The problem with this definition is that the functions are not instances of Show
and will give you an error ( here the explanation ). You can use the instance definition of Text.Show.Functions
as an alternative:
Prelude> import Text.Show.Functions
Prelude Text.Show.Functions> data Usuario a b = Usuario String [a -> b] deriving Show
Prelude Text.Show.Functions> let x = Usuario "Hola" [\x -> True, \x -> False]
Prelude Text.Show.Functions> x
Usuario "Hola" [<function>,<function>]