- Boolean Values and Expressions
- More Boolean Operations
- Zipping Lists
- Input / Output
- I/O Monad
- Installing Haskell
- Boolean Values are either
TrueandFalse. - 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,
<,<=,>,>=.
elemfunction 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
notfunction returns the opposite boolean value, the logical complement.
not True -- > False
not (not False) -- > False&&infix operator is a boolean conjunction (ANDfunction).
True && True -- > True
False && True -- > False||infix operator is a boolean disjunction (logicalOR); dual of theANDoperation.
True || False -- > True
False || False -- > Falsexorfunction returns true when its two boolean arguments are different.
True `xor` False -- > True
False `xor` False -- > False- Haskell supports multi-input boolean operations with
andandorfunctions that take a list of boolean values as a single input.
and [False, True, False, True] -- > False
or [True, True, False] -- > Trueifexpressions evaluate to either thethenvalue or theelsevalue, based on theifvalue.
if 2*2==4 then "happy" else "sad" -- > "happy"
if True then 42 else pi -- > 42.0zipfunction combines a pair of lists into a list of pairs.- If
zipfunction is used on lists which are of different lengths, length of the output list will be the length of the shortest list.
- If
zip3function combines three lists into a triplet.- By adding
import Data.List, we get pre-baked definitions ofzip3...zip7andzipWith3...zipWith7. zipWithfunction is a generalization ofzip.- While
zipcan only combine elements from two input lists by creating pairs of those elements,zipWithallows us to provide a function that tells us how to combine the elements from the input lists. zipWithfunction applies a function to the result ofzip.zipWithfunction 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
getLineand output is withputStrLn. putStrLn(likeprintlnin Java orprintin Python) prints a character string to the terminal.getLinefunction reads in a character string from user input.- After
getLinefunction is invoked, text should be typed at the>prompt followed by pressing the enter key.
- After
readreads 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:
showtakes a value and returns a String representation of that value.showfunction is the dual of thereadfunction.
readandshowfunctions synthesize values from Strings and vice versa.printdoes the composition ofputStrLnandshow.
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
doto 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: