haskell chapter 1, part i. highly recommended learn you a haskell for great good. miran lipovaca

14
Haskell Chapter 1, Part I

Upload: evan-leonard

Post on 29-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Haskell

Chapter 1, Part I

Page 2: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Highly Recommended Learn you a Haskell for Great Good. Miran

Lipovaca

Page 4: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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

Page 5: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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

Page 6: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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

FUNCTIONINPUTS

OUTPUTS

Page 7: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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

someFunction5 45

someFunction5 still (and always) 45… and

no other effects

Allows Haskell to do memoization

Page 8: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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

Page 9: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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. 

Page 10: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Haskell is elegant and concise Can do a lot with a few lines of code… but it

might take some getting used to!

Page 11: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Quick Haskell Demo Simple arithmetic (+, *, -, /, `div`,

precedence) Booleans (True, False, &&, ||, not) Comparisons (==, /=) succ/pred

Page 12: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

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

Page 13: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Quick Haskell DemoPut function definitions in a fileFunction 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

Page 14: Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Play and Share1. 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.