Home > front end >  haskell another function as input parameter
haskell another function as input parameter

Time:03-25

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.

CodePudding user response:

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 *)
  • Related