- Boolean Values and Expressions
- More Boolean Operations
- Zipping Lists
- Input / Output
- I/O Monad
- Installing Haskell
- Boolean Values are either
True
andFalse
. - Double-equals operator
==
is used for testing value equality. - Not-equals operator is
/=
(looks like an equals sign with a line through it). - Haskell cannot compare two values that have different types.
- Haskell supports the standard comparison / relational operators,
<
,<=
,>
,>=
.
elem
function returns true if a value is part of a list, and false otherwise.
elem 1 [1,2,3] -- > True
- Haskell permits any two-argument function to be written as an infix operator using backquote (`) characters.
1 `elem` [1,2,3] -- > True
42 `max` 13 -- > 42
(+) 1 1 -- > 2
- The
not
function returns the opposite boolean value, the logical complement.
not True -- > False
not (not False) -- > False
&&
infix operator is a boolean conjunction (AND
function).
True && True -- > True
False && True -- > False
||
infix operator is a boolean disjunction (logicalOR
); dual of theAND
operation.
True || False -- > True
False || False -- > False
xor
function returns true when its two boolean arguments are different.
True `xor` False -- > True
False `xor` False -- > False
- Haskell supports multi-input boolean operations with
and
andor
functions that take a list of boolean values as a single input.
and [False, True, False, True] -- > False
or [True, True, False] -- > True
if
expressions evaluate to either thethen
value or theelse
value, based on theif
value.
if 2*2==4 then "happy" else "sad" -- > "happy"
if True then 42 else pi -- > 42.0
zip
function combines a pair of lists into a list of pairs.- If
zip
function is used on lists which are of different lengths, length of the output list will be the length of the shortest list.
- If
zip3
function combines three lists into a triplet.- By adding
import Data.List
, we get pre-baked definitions ofzip3
...zip7
andzipWith3
...zipWith7
. zipWith
function is a generalization ofzip
.- While
zip
can only combine elements from two input lists by creating pairs of those elements,zipWith
allows us to provide a function that tells us how to combine the elements from the input lists. zipWith
function applies a function to the result ofzip
.zipWith
function can take a lambda function for the operation.
- While
zip [1,2,3] [4,5] -- > [(1,4),(2,5)]
zipWith max [1,2,3] [0,2,4] -- > [1,2,4]
zipWith (+) [1,2,3] [0,2,4] -- > [1,4,7]
zipWith (\x->(\y->(x,y))) [1,2,3] "abc" -- > [(1,'a'),(2,'b'),(3,'c')] -- Note: Strings in Haskell are list of characters
- Input is with
getLine
and output is withputStrLn
. putStrLn
(likeprintln
in Java orprint
in Python) prints a character string to the terminal.getLine
function reads in a character string from user input.- After
getLine
function is invoked, text should be typed at the>
prompt followed by pressing the enter key.
- After
read
reads values as strings, and converts them into other types.- A type annotation (for example:
::Int
) is mandatory; otherwise it is not clear what type of number the input String is meant to represent.
- A type annotation (for example:
show
takes a value and returns a String representation of that value.show
function is the dual of theread
function.
read
andshow
functions synthesize values from Strings and vice versa.print
does the composition ofputStrLn
andshow
.
putStrLn ("hello " ++ "world" ++ "!!") -- > "hello world!!" -- :: String
do {
putStrLn "what is your name?"
x <- getLine
putStrLn ("hello " ++ x)
}
read "42" :: Int -- > 42 -- :: Int
show 42 -- > "42" -- :: String
print 42 -- > 42 -- :: IO ()
- Input and output (I/O) operations are impure.
- Use
do
to specify a sequence of actions. - Use
<-
inside a do to associate input values with names. - Any value or function that involves I/O has IO in its type.
- Note:
<-
is for associating names with values in do blocks;- Whereas
->
is used for defining functions.
- Whereas
let greet() = do
planet <- getLine
home <- getLine
putStrLn ("greetings " ++ planet ++ "ling.")
putStrLn ("I am from " ++ home ++ ".")
putStrLn "Take me to your leader."
Two bundled distributions of GHC are: