jian
jian

Reputation: 4877

haskell another function as input parameter

hi_copy.hs

waxOn = x
    where 
        z = 7
        y = z + 18
        k = y ^ 2
        x = k * 5

triple  = a
    where 
        a = a * 3 

then load.

ghci> :load hi_copy.hs
[1 of 1] Compiling Main             ( hi_copy.hs, interpreted )
Ok, one module loaded.

then run triple waxOn

<interactive>:122:1: error:
    • Couldn't match expected type ‘Integer -> t’
                  with actual type ‘Integer’
    • The function ‘triple’ is applied to one value argument,
        but its type ‘Integer’ has none
      In the expression: triple waxOn
      In an equation for ‘it’: it = triple waxOn
    • Relevant bindings include it :: t (bound at <interactive>:122:1)

run 3 * waxOn will work. but now I don't how to make triple waxOn working. meta: till now still not learning type in haskell. There maybe other good answers already exists. But I don't understand other people's great answer.

Upvotes: 1

Views: 52

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477606

Your triple does not take any parameter. It only returns a value, a value that will be the result of endless recursion where a is ((((…) * 3) * 3) * 3).

You should take as input a parameter, so:

triple :: Num a => a -> a
triple a = a * 3

or shorter:

triple :: Num a => a -> a
triple = (3 *)

Upvotes: 3

Related Questions