# CSE 130 Lecture Notes - Lecture 16: Type Class, Identity Function, Type Safety

24 views4 pages
7 Mar 2018
School
Course
Professor
Back to call/cc
800 x = …
801 - call/cc (K => { console.log(“A)”; ← executed immediately
If (x > 55) { loop forever }
Else { k() }
}
802 console.log(“B”)
NEEDS Runtime support! - or else how it would it know to continue onto 802?
Remember: type IO a = World -> (a, World)
“De-sugar” getChar :: IO Char World → (Char, World)
-”given a World, give you a Char, and a new World with the Char in it”
putChar :: Char -> IO () Char → World → ((), World)
How do we create actions?
Return :: a → IO a a → World → (a, World)
Ex: return x w = (x, w)
x :: a
w :: world
Compose (>>) :: IO a → IO b → IO b (World -> (a, World)) → (World → (b, World)) →
(World → (b, World))
(>>) act1 act2 world0 =
Let ( _, w1 ) = act1 world0
( y, w2 ) = act2 w1
In ( y, w2 )
Example: return 42 >> putChar ‘A’ >> putChar ‘B’ //prints ‘A’ and ‘B’ to the terminal (discards 42)
f x = putStrLn “hello world” >>
If x == “hello”
Then return x
Else return “bye bye!”
f :: String → IO String
f “hello” = “hello”
The Bind function (>>=)
-similar to (>>), but does NOT throw away the result
Type: (>>=) :: IO a → (a → IO b) → IO b
Define (>>) in terms of (>>=): x >> y = x >>= \a → y
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

# Get OneClass Notes+

75% OFF
\$8 USD/m
###### Monthly
\$30 USD/m
You will be charged \$96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.