Reputation: 28861
I want to create a new data type that can take in a function. I know this works:
foo :: (Int -> Int -> Int)->Int->Int->Int
foo func x y = (func x y) + 100
so you can do this:
foo (+) 5 8 --output is 113
Now I want to do the same thing but to a custom data type like this:
data Bar = Bar (Int Int Int) Int Int --This does not compile
so I can use it like this:
doCalc :: Bar -> Int
doCalc (Bar func x y) = (func x y) + 100
My question is how do I declare the constructor in my data type to do this?
Upvotes: 0
Views: 56
Reputation: 3428
Note that the first argument of foo
is of type (Int -> Int -> Int)
- the type of a function that takes two Int
s and return another one. (Int Int Int)
is not a legal Haskell type (since Int
is a concrete type and not a type constructor, like Maybe
).
Try:
data Bar = Bar (Int -> Int -> Int) Int Int
Your doCalc
function is correct and will work with the new version.
Upvotes: 3