Introduction to Haskell Programming Languages - Round 3 Autumn 2015

Slide Note
Embed
Share

Explore the Octopus Interpreter project for CSE 341 Programming Languages course. Reminder on Monads in Haskell and dos and don'ts in code conversion. Dive into scope understanding with examples of printing and mapping in Haskell functions.


Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CSE 341 : Programming Languages Section 5 Haskell: Round 3 Autumn 2015

  2. Octopus Interpreter... Due Tomorrow! It s really okay to use late days on this one! Any pressing questions? Office hours remaining: Justin Adsuara (justbads at cs) Section AC: Thurs 2:30pm-3:20pm SAV 130 Office Hours: Thurs 3:30-4:20, CSE 002 2

  3. Reminder: Monads class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a Remember these! Monads do all sorts of things in Haskell. We ve seen some IO, but there s also [random, graphics, HTTP ..] 3

  4. Some dos and donts Let s convert! printsqrt2 = do putStr "the square root of 2 is " putStrLn (show (sqrt 2)) calcsqrt = do x <- readLn putStrLn "finding the square root of x" putStrLn (show (sqrt x)) 4

  5. Some dos and donts Let s convert! printsqrt2 = do putStr "the square root of 2 is " putStrLn (show (sqrt 2)) printsqrt2 = putStr "the square root of 2 is " >> putStrLn (show (sqrt 2)) 5

  6. Some dos and donts Let s convert! calcsqrt = do x <- readLn putStrLn "finding the square root of x" putStrLn (show (sqrt x)) calcsqrt = readLn >>= \x -> putStrLn "finding the square root of x" >> putStrLn (show (sqrt x)) Questions? 6

  7. Lets Keep Things in Scope! What does this print? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs dostuff xs 7

  8. Lets Keep Things in Scope! What does this print? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs 11 12 dostuff xs 8

  9. Seriously, Lets Keep Things in Scope! How about if Haskell were dynamically scoped? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs dostuff xs 9

  10. Seriously, Lets Keep Things in Scope! How about if Haskell were dynamically scoped? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs 11 13 dostuff xs 10

  11. Seriously, Lets Keep Things in Scope! How about if Haskell were dynamically scoped? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs 11 13 dostuff xs 11

  12. Seriously, Lets Keep Things in Scope! How about if Haskell were dynamically scoped? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs Or maybe it would hang... dostuff xs 12

  13. Seriously, Lets Keep Things in Scope! How about if Haskell were dynamically scoped? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs Or, um... dostuff xs 13

  14. Seriously, Lets Keep Things in Scope! How about if Haskell were dynamically scoped? x = 10 xs = map (x+) [1..] printout (x:xs) = putStrLn $ show x dostuff (x:xs) = do putStrLn $ show x printout xs Yeah, this doesn t really work with lazy evaluation. dostuff xs 14

  15. Speaking of Lazy Evaluation... Define a list containing all integers. 1. As a non-recursive data structure 2. As a recursive data structure 15

  16. Speaking of Lazy Evaluation... Define a list containing all integers. 1. As a non-recursive data structure nonzeroints = 0:foldr1 (++) [[x,(-x)] | x <- [1..]] 2. As a recursive data structure 16

  17. Speaking of Lazy Evaluation... Define a list containing all integers. 1. As a non-recursive data structure nonzeroints = 0:foldr1 (++) [[x,(-x)] | x <- [1..]] 2. As a recursive data structure nonzeroints = 0 : map (\x -> if (x > 0) then (-x) else (1 - x)) nonzeroints 17

  18. Any Haskell/Octopus questions? Tomorrow we start Prolog! 18

Related


More Related Content