Download presentation
Presentation is loading. Please wait.
Published byEvan Leonard Modified over 9 years ago
1
Haskell Chapter 1, Part I
2
Highly Recommended Learn you a Haskell for Great Good. Miran Lipovaca
3
Why are we doing this? http://stackoverflow.com/questions/3175656/why-should- i-want-to-learn-haskell http://stackoverflow.com/questions/3175656/why-should- i-want-to-learn-haskell http://programmers.stackexchange.com/questions/25569/i s-haskell-worth-learning http://programmers.stackexchange.com/questions/25569/i s-haskell-worth-learning
4
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
5
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
6
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) info from Wikipedia FUNCTION INPUTS OUTPUTS
7
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 545 someFunction 5still (and always) 45… and no other effects Allows Haskell to do memoization
8
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
9
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.
10
Haskell is elegant and concise Can do a lot with a few lines of code… but it might take some getting used to!
11
Quick Haskell Demo Simple arithmetic (+, *, -, /, `div`, precedence) Booleans (True, False, &&, ||, not) Comparisons (==, /=) succ/pred
12
Quick Haskell Demo - continued definition (let) Lists [1,2,3] homogeneous common to concatenate (++) cons/add to front ( : ) access element by index ( !! ) [use sparingly, to get more FP feel] lists of lists comparing lists list parts: head, tail, init, last, length take, drop, maximum, minimum, sum, product, elem Ranges
13
Quick Haskell Demo Put function definitions in a file Function names must begin with lowercase!! 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) WinGHCi fine for small examples, mostly you will edit/load files
14
Play and Share 1. doubleHead takes two lists and returns the product of the heads. doubleHead [2,3] [4,6] => 8 2. 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) 3. 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? 4. 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. 5. 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] 6. 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] 7. removeHeads takes two lists and concatenates them, minus the first elements. removeHeads [1,2,3] [4,5,6] => [2,3,5,6] 8. 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. grabIt wordLengths word 2 => “cat” HINT: You’ll need to use (fromIntegral …) to convert from Integer to Int. More in chapter 2.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.