f1 = putStrLn "Hello World"
f2 = print 42
Prelude> :t putStrLn
putStrLn :: String -> IO ()
Prelude> :t print
print :: (Show a) => a -> IO ()
Prelude> :t readLn
readLn :: (Read a) => IO a
IO a stands for an action which gives a result of type aunsafePerformIOIn short, Haskell is the world’s finest imperative programming language - Simon Peyton Jones, Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell
main = do
n <- readLn
print $ n + 1
main = do
n <- readLn
let ans = 42
print $ n + ans
main = do
n <- readLn
print $ n + 1
main is the only action which is run
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
>>= looks like a pipeline feeding results from one computation to the next>> discards the results, used only for sequencereturn is only a functionmain = do
return 4
print 5
instance Monad [] where
m >>= k = foldr ((++) . k) [] m
m >> k = foldr ((++) . (\ _ -> k)) [] m
return x = [x]
g l1 l2 = [(x, y) | x <- l1, y <-l2]
f l1 l2 = do
x <- l1
y <- l2
return (x, y)
f l1 l2 = do
x <- l1
y <- l2
return $ x + y
f readLine readLine
f (Just 3) (Just 4)
f (Just 3) Nothing
Maybe a : at most one value of type a[a] : several values of type aIO a : action with result of type a and some side-effectsSTM a : subset of IO a for paralellismState, Reader, Writer, Parsec, etcimport Graphics.Rendering.Cairo
import Graphics.UI.Gtk
main = do
initGUI
window <- windowNew
widgetShowAll window
mainGUI
import Graphics.Rendering.Cairo
import Graphics.UI.Gtk
main = do
initGUI
window <- windowNew
window `on` deleteEvent $ liftIO mainQuit >> return False
widgetShowAll window
mainGUI
on connects a function to a signalmainQuit will signal GTK to close everythingdeleteEvent expects a Bool valueimport Graphics.Rendering.Cairo
import Graphics.UI.Gtk
main = do
initGUI
window <- windowNew
window `on` deleteEvent $ liftIO mainQuit >> return False
windowSetGeometryHints window (Nothing :: Maybe Window)
(Just (800, 600)) (Just (800, 600)) Nothing Nothing Nothing
set window [ windowTitle := "First GUI app" ]
widgetShowAll window
mainGUI
counter :: Behavior Int
counter = accumulate ($) 0
(fmap (+1) eventUp
`union` fmap (subtract 1) eventDown)