K Lawson
K Lawson

Reputation: 21

Haskell Lambda Functions

I am new to Haskell and programming in general. I am trying to write a lambda function that return a value squared

(\x  -> x * x) 

this is the code I have written. When i try to compile I get this error Parse error: naked expression at top level Perhaps you intended to use TemplateHaskell

I have googled it I cannot fund a solution

Upvotes: 2

Views: 543

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

You should assign this to a identifier, since \x -> x * x as program does not make much sense: you define a function, but then you throw that function away.

You thus can implement a function:

mysquare :: Num a => a -> a
mysquare = \x -> x * x

then you can call the function when necessary. We can for example define a main with:

main :: IO ()
main = putStrLn (show (mysquare 5))

and then call main:

Prelude> main
25

Upvotes: 1

Related Questions