Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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.

>Solution :

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 *)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading