Yahya Uddin
Yahya Uddin

Reputation: 28861

Create a haskell constructor for a new data type that takes in a function

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

Answers (1)

Benesh
Benesh

Reputation: 3428

Note that the first argument of foo is of type (Int -> Int -> Int) - the type of a function that takes two Ints 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

Related Questions