Babas
Babas

Reputation: 395

Haskell High Order Functions

I'm a little bit confuse about high order function and I wanted to do a stupid example to see if I understood well but still I can't compile without errors, i.e I guess I'm missing something. My very basic example is as follow, I create a function that adds up two number, and then I create an other function which uses my previous function and sum up an other number.

Sum2 :: Double -> Double -> Double  
Sum2  a b = a + b

Sum3 :: (Double -> Double -> Double) -> Double -> Double    
Sum3 a b c = (Sum2 a b) + c


test1:: Double
test1 = Sum3 2.0 3.0 4.0

Upvotes: 0

Views: 73

Answers (1)

chi
chi

Reputation: 116174

There is no question, but I'll guess the OP wants some explanation about why this is not working:

Sum3 :: (Double -> Double -> Double) -> Double -> Double    
Sum3 a b c = (Sum2 a b) + c

First, functions should have a lowercase name. Second, the type above is the type of a function with two arguments:

sum3 :: (Double -> Double -> Double) -> Double -> Double    
        ----- first arg ------------    -2nd--    -result type-

So, one should write

sum3 f a = ...

where f is the function passed as first argument Double -> Double -> Double, and a is the second argument number. Since f is a function taking two arguments, we need to use it accordingly, e.g.

sum3 f a = f a a
-- or
sum3 f a = f a 12
-- or
sum3 f a = f (a+12) (f 32 a)

I have no idea about what you are trying to achieve, the above ones are only examples.

Finally, the name sum3 is unintuitive, we should use something else.

Upvotes: 1

Related Questions