- pure functions, no side effects, good for reasoning
- IO system separates pure functions from side-effects
- Hello world, various ways to run it
putStrLn
andgetLine
, do notation- Types of
IO ()
andIO String
- Reverse example, with IF and recursive call
- Values you can pass around, executed by
main
(or in the REPL, or by unsafePerformIO)
putChar
,putStr
,print
,getChar
when
sequence :: [IO a] -> IO [a]
mapM
andmapM_
(map and then sequence)forM
forever
getContents
interact
openFile
,hGetContents
,hClose
, andwithFile
hGetLine
,hPutStr
,hPutStrLn
,hGetChar
readFile
,writeFile
,appendFile
doesFileExist
hSetBuffering
,BufferMode
(NoBuffering
,LineBuffering
,BlockBuffering (Maybe Int)
),hFlush
.openTempFile
,removeFile
,renameFile
### From System.Environment
getProgName
,getArgs
Run stack install random
or cabal install random
random
,randoms
,randomR
,randomRs
RandomGen
,Random
StdGen
,mkStdGen
,getStdGen
,newStdGen
E.g.,
random (mkStdGen 100) :: (Bool, StdGen)
Data.ByteString
(strict) andData.ByteString.Lazy
.Word8
(range 0-255)pack
,unpack
cons
(lazy),cons'
(strict),empty
fromChunks
,toChunks
- functions with same name as those in
List
- functions with same name as
System.IO
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString as S
- as can pure code (such as
div 4 0
orhead []
) - can be caught in IO only
catch
(fromimport Control.Exception.Base
)IOError
isDoesNotExistError
(inSystem.IO.Error
),isAlreadyExistsError
...userError
,ioError
From: https://github.com/noelmarkham/learn-you-a-haskell-exercises/blob/master/09-input-and-output.hs
{- Write a lottery number picker
- This function should take a StdGen instance, and produce a list of six unique numbers between 1 and 49, in numerical order
-}
lottery :: StdGen -> [Int]
lottery gen = undefined
Draw an ASCII xmas tree. E.g., combine triangle(s) and rectangle. The program should accept the height of the tree as input.
Inspiration: https://duckduckgo.com/?q=ascii+christmas+tree&t=ffab&iax=1&ia=images
E.g.,
How tall a tree? 3
*
***
*****
|