mapm putchar josh bicking what s haskell
play

mapM_ putChar "Josh Bicking" Whats Haskell? Haskell is - PowerPoint PPT Presentation

mapM_ putChar "Josh Bicking" Whats Haskell? Haskell is a functional, lazy, pure language. Functional Program logic is functions and data (and functions as data). Focused on statelessness: instead of changing


  1. mapM_ putChar "Josh Bicking"

  2. What’s Haskell? ● Haskell is a functional, lazy, pure language. ● Functional ○ Program logic is functions and data (and functions as data). ○ Focused on statelessness: instead of changing variables, you call functions, which call other functions, and so forth. ● Lazy ○ Nothing is evaluated until it’s needed. ○ The value of unused variables isn’t calculated. x = 1/0 won’t throw an error, unless you try to use x ! ○ ● Pure ○ Variable and function names can’t be overwritten once set. x = x+1 makes no sense. ○

  3. Try Haskell yourself! Any lines starting with λ> can be given to a Haskell interpreter. ● ○ You can follow along and try things yourself at https://repl.it/languages/haskell ■ Be sure you type into the interpreter (the terminal prompt). The left part is for writing executables. ○ If you’re feeling more adventurous, download and install Haskell through stack: https://www.haskellstack.org/ Once it’s complete, open an interpreter with stack ghci ■

  4. Syntax and Structure

  5. Goodbye, S-expressions! ● λ> 2 + 2 Lisp haters rejoice: Haskell tries to avoid those dreaded parentheses. 4 Some functions, like + , have a special ● λ> (+) 2 2 -- prefix notation prefix and infix notation. 4 ○ Most just have a prefix notation. λ> quot 33 5 ○ Functions without a special infix 6 notation may be used infix by λ> 33 `quot` 5 -- using functions as infix surrounding them with backticks. 6

  6. Type Signatures ● λ> :t replicate Structure ○ 0 or more inputs that result in an output. replicate :: Int -> a -> [a] ○ Can specify data types or type restrictions. λ> :t (+) ● Data types (+) :: Num a => a -> a -> a ○ Takes data of that input type. λ> :t (< 3) ● Type restrictions (< 3) :: (Num a, Ord a) => a -> Bool ○ Takes data that satisfies the category λ> :t map restrictions placed on the input. ● Higher Order Functions map :: (a -> b) -> [a] -> [b] ○ Functions given as data are subject to the same type signatures.

  7. Definitions ● Everything is a function. Mostly. x is just data. However, its type ○ λ> x = 5 signature suggests it’s a function that λ> :t x takes no arguments and returns a number. x :: Num t => t ○ Functions and “data” are declared the λ> squaredAdd a b = a^2 + b^2 same way. λ> :t squaredAdd ● Note: The interpreter will let you squaredAdd :: Num a => a -> a -> a “redefine” x . This is for convenience in λ> squaredAdd 2 3 the interpreter, and not allowed in 13 compiled Haskell. Also, x= x+1 still won’t work like it ○ does in other languages.

  8. Flow Control λ> if 5 == 6 then "foo" else "bar" "bar" ● We have a familiar looking if . λ> isEmpty l = case l of; [] -> True; otherwise -> False λ> isEmpty [1,2,3] ● Also have an interesting case . False ○ λ> isEmpty [] Uses Pattern Matching : data matches a specific True structure. ● Conditionals and pattern -- Case statements look nicer outside of the interpreter. matching can also be used as isEmpty l = part of a function definition. ● Side note : Outside of the case l of interpreter, Haskell is [] -> True structured with indentation otherwise -> False and line breaks.

  9. . and $ a b c d e -- "Call function a, with arguments b, c, d, e" ● Haskell lets you keep structure, without throwing in tons of parentheses. a b (c d e) -- "Call function a, with arguments b, ($) : Give precedence to the right of ○ and the result of calling c with arguments d, e." the $ . a b $ c d e -- The same as above. (.) : Chain functions together: take ○ the output of the right, and apply it as an argument to the left. a (b (c d)) -- "Call c with arguments d, e. Apply the ■ Meant to look like the result of c to b, then apply the result of b to a. mathematical function (a . b . c) d -- The same as above. composition operator, ∘ . a . b . c $ d -- The same as above.

  10. Why use Haskell?

  11. The Theory Underneath ● Haskell is based off some really cool constructs! ○ Category Theory ○ Theoretical Computer Science ○ Programming language theory ● I won’t go too deep into these, just why they help Haskell do what it can do. ○ The theoretical constructs give Haskell a lot of practical advantages.

  12. Strict, extensive type system ● λ> fun1 a = a No casting ○ Turning an Integer into a Double requires a λ> :t fun1 function that takes an Integer and returns a fun1 :: p -> p Double. ● Typeclasses are (optionally) inferred by the λ> fun2 a b = a < b compiler. λ> :t fun2 ○ Typeclass is determined by how the data is fun2 :: Ord a => a -> a -> Bool used. λ> fun3 a = a < 3 λ> :t fun3 fun3 :: (Ord a, Num a) => a -> Bool

  13. Referential Transparency ● λ> f a b = a + b You may substitute the right hand side of a declaration, in any context. λ> x = 3 ○ The meaning doesn’t change. λ> y = 5 ● Immutability guarantees a function’s result is determined only by its input. λ> f x y ○ No concept of state! 8 ● Cool use case: “Hotswapping Haskell” for λ> f 3 5 -- Substitute x and y Facebook’s spam filter 8 ○ Functions are updated on the fly. λ> x + y -- Substitute f ■ New objects are swapped in. 8 ■ Old objects are marked for garbage collection.

  14. Parallelism is Easy! import Control.Parallel (par) factorial n = product [1..n] let ● Functions don’t modify each other, so we x = factorial 20000 can run them simultaneously without y = factorial 30000 worrying. par a b lets you evaluate a and b in ○ simultaneously. par x (par y (x - y)) For those of you in an interpreter (this probably won’t work on repl.it): λ> import Control.Parallel (par) λ> factorial n = product [1..n] λ> let { x = factorial 20000; y = factorial 30000 } in par x (par y (x - y))

  15. Laziness: It’s a good thing ● λ> x = [1..] Elements that are never used are never evaluated. λ> x !! 10 ● Declare a huge, or infinite list, and take what 11 you need from it. ○ A program to solve Sudoku , by Richard Bird λ> take 5 x sudoku :: Board -> [Board] ■ [1,2,3,4,5] ■ For any board configuration, compute all possible ways to fill it. λ> show x -- This would loop forever!

  16. Why use Haskell? A program becomes a number of side-effect free, strongly typed functions. This leaves very little room for runtime errors.

  17. A Touch of Theory: The Type System

  18. Duck Typing on Steroids ● Duck typing : “If it waddles and quacks like a duck, then it’s probably a duck.” Python also uses “duck typing”. ○ The type of data is inferred: it doesn’t have to be specified. >>> x = 3 Let’s say we have a Duck d . It can waddle. ● >>> type(x) ○ Python <class 'int'> d.waddle() - ✓ ■ d.ribbit() - Runtime error ■ >>> x = 3.0 ○ Haskell >>> type(x) ■ d is a Duck data type, and Duck is part of the Waddles <class 'float'> - ✓ typeclass. ■ d is a Duck data type, and Duck is not part of the Ribbits typeclass. - Compile time error.

  19. Category Theory in the Type System Because there aren’t any papers on Duck Typing Theory. ● If a data type can implement what’s necessary to be in a typeclass, then it belongs to that class Eq a where typeclass. (==), (/=) :: a -> a -> Bool ○ In Haskell, typeclasses are defined with class (not to be confused with a Java class). -- Minimal complete definition: To be in Eq , a data type must implement ○ (==) and (/=) , and their results must not be -- (==) or (/=) equal to each other. x /= y = not (x == y) ● Offers data encapsulation and polymorphism x == y = not (x /= y) without an OOP model.

  20. Something is missing... I’ve left out an essential part of learning a new programming language. module Main where main :: IO () Printing requires IO, and IO is a side effect: it main = putStrLn "Hello world!" changes the state of a system. Haskell abstracts away side effects through monads.

  21. Monads: Bundling State

  22. var person = { "name":"Homer Simpson", Let’s look at some "address": { "street":"123 Fake St.", JavaScript "city":"Springfield" } }; if (person != null && person["address"] != null) { ● This code is riddled with null checks. var state = person["address"]["state"]; ○ Is there any way we can remove them? if (state != null) { Haskell has a Maybe data type. ● console.log(state); } data Maybe t = Just t | Nothing else { console.log("State unknown"); A Maybe has some value wrapped in a ● } Just , or it has no value, Nothing . }

  23. Maybe in JavaScript var Nothing = {}; var Maybe = function(value) { var Just = function(value) { return function() { return value; ● Now we have a unit function }; Returns a Nothing object if given null or ○ undefined . }; Returns a Just function if given a value, ○ which returns the original value. if (typeof value === 'undefined' || value === null) return Nothing; return Just(value); };

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend