What is func in this code block?

47 views
Skip to first unread message

sleepy hollow

unread,
Jul 4, 2017, 1:34:35 AM7/4/17
to nicta-fp
Apols if this is too basic, initially I thought func must be a reserved word in haskell.

when loaded and executed num3Times4 returns 12.
func appears to call times4.

is func a reserved word? how does this code know func is supposed to call times4 ?
------------------------------------------------------------------
times4 :: Int -> Int
times4 x = x * 4

doMult :: (Int -> Int) -> Int
doMult func = func 3


num3Times4 = doMult times4
--------------------------------------------------------------------

sleepy hollow

unread,
Jul 4, 2017, 2:34:34 AM7/4/17
to nicta-fp
oh bah... realized the obvious.

the call by num3Times4 nominates a function to execute.
in this example I replaced func with foo to demonstrate func was not a reserved word.



------------------------------------------------------------------------

times4 :: Int -> Int
times4 x = x * 4

times5 :: Int -> Int
times5 x = x * 5

times6 :: Int -> Int
times6 x = x * 6

doMult :: (Int -> Int) -> Int
-- (internal function receives an Int, returns an Int)
-- function receives an Int & returns an Int
--doMult func = func 3
doMult foo = foo 3


num3Times4 = doMult times4
num3Times5 = doMult times5
num3Times6 = doMult times6

------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages