We can use the let
keyword to give an expression a name by which we can refer to it later. Naming an expression may help clarify the meaning of code and can reduce duplication when the same expression is written more than once.
=
main do
If we let x equal 2, then x plus x equals 4.
let x = 2
putStrLn (show (x + x))
You can define multiple variables at once.
let (b, c) = ("one", "two")
putStrLn b
putStrLn c
You can also define multiple variables within a single let
clause.
let
= True
d = [1,2,3]
e putStrLn (show d)
putStrLn (show e)
$ runhaskell variables.hs
4
one
two
True [1,2,3]
Next: Defining functions