130 likes | 371 Views
Haskell. Chapter 1, Part I. Why are we doing this?. http://stackoverflow.com/questions/3175656/why-should-i-want-to-learn-haskell http://programmers.stackexchange.com/questions/25569/is-haskell-worth-learning. Why study functional languages?.
E N D
Haskell Chapter 1, Part I
Why are we doing this? • http://stackoverflow.com/questions/3175656/why-should-i-want-to-learn-haskell • http://programmers.stackexchange.com/questions/25569/is-haskell-worth-learning
Why study functional languages? Without understanding functional programming, you can’t invent MapReduce, the algorithm that makes Google so massively scalable. —Joel Spolsky, The Perils of Java Schools
Imperative Programming Language • The design of the imperative languages is based directly on the von Neumann architecture • Computer follows a sequence of task specified by the programmer • Focus is on state – variables that control the problem solution • Use flow-control to work toward solution • sequence • decision (if/else) • looping (for/while/etc) 0x0000 0x0010 0x0100 0x0110 …. instructions data
Functional Programming Language • Emphasizes functions that provide results • Avoids state and mutable data • Relatively unconcerned with the architecture of the machines on which programs will run • Has its roots in lambda calculus (covered later) OUTPUTS INPUTS FUNCTION info from Wikipedia
Referential Transparency • In a purely functional language, functions have no side effects • As a result, if you call the same function twice with the same parameters, it is guaranteed to produce the same results • This is called referential transparency • Enables you to prove that a function is correct someFunction 5 45 someFunction 5 still (and always) 45… and no other effects
Lazy evaluation • Haskell doesn’t evaluate functions until it needs to show a result • OK with referential transparency… no one is relying on any side effects • Enables infinite data structures…. only parts you want to display are actually computed
Statically typed with type inference • Knows the type of a piece of code at compile time (static) • Won’t allow you to add a string and number, for example • Type system uses type inference. Haskell figures out the type (e.g., a = 5 + 4, a must be a number) • Allows programmers to leave out type declarations but still do type checking • Is not the same as dynamic typing – why not? • Some languages with type inference: • ML, OCaml, F#, Haskell, Scala, D, Clean, Opa and Go.
Haskell is elegant and concise • Can do a lot with a few lines of code… but it might take some getting used to!
Quick Haskell Demo • Simple arithmetic (+, *, -, /, `div`, precedence) • Booleans (True, False, &&, ||, not) • Comparisons (==, /=) • succ/pred
Quick Haskell Demo - continued • definition (let) • Lists [1,2,3] • homogeneous • common to concatenate (++) • cons/add to front ( : ) • access element by index ( !! ) • lists of lists • comparing lists • list parts: head, tail, init, last, length • take, drop, maximum, minimum, sum, product • Ranges
Quick Haskell Demo Put function definitions in a file Load • Function calls • Function definitions • every function must return a value • function names can’t begin with capital letters • use apostrophe at end to denote strict (not lazy) or slightly modified version of a file • if/else • else is required (statement must do something)
Play and Share • doubleHead takes two lists and returns the product of the heads. doubleHead [2,3] [4,6] => 8 • longerList takes two lists and returns the longer one. longerList [1,2][4,5,6] => [4,5,6] (your choice which to return if same length) • isItThere takes an element and a list and returns “yes” if element is in list, “no” otherwise. isItThere 1 [2,3] => “no” Can you make this work with a string? • evenOdd num takes a number n and returns a list containing the first n even numbers followed by the first n odd numbers. evenOdd 4 => [2,4,6,8,1,3,5,7]. You may assume n <= 100. • addToTail takes two lists and returns a list with the head of the first list at the end of the second. addToTail [1,2,3] [4,5,6] => [4,5,6,1] • replaceLast takes two lists and returns a list with the last element of the second list replaced by the last element of the first list. replaceLast [1,2,3] [4,5,6] => [4,5,3] • removeHeads takes two lists and concatenates them, minus the first elements. removeHeads [1,2,3] [4,5,6] => [2,3,5,6] • replaceList takes two lists and replaces the first n elements (where n = length of list 1) with the elements of list 1. replaceList [1,2] [4,5,6,7] => [1,2,6,7] replaceList[1,2,3,4] [7,8] => [1,2,3,4] Advanced • grabIt. Takes a string of words, no spaces, e.g., “theblackcat”. Takes a list of word lengths, e.g., [3,5,3]. Takes a word number, e.g., 1, 2 or 3. returns the word. grabItwordLengths word 2 => “cat” • HINT: You’ll need to use (fromIntegral …) to convert from Integer to Int. More in chapter 2.