Download Haskell 1A

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of the function concept wikipedia , lookup

Transcript
Haskell
Chapter 1, Part I
Highly Recommended

Learn you a Haskell for Great Good. Miran Lipovaca
Why are we doing this?

http://stackoverflow.com/questions/3175656/why-shouldi-want-to-learn-haskell

http://programmers.stackexchange.com/questions/25569/i
s-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
instructions
0x0010
0x0100
0x0110
….
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
Allows Haskell to do memoization
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!
GETTING HELP
 If you need help on a lab, it’s best to send me your code.



In 261, issues tend to be syntax/easy.
In 262/306 I know common mistakes
With Haskell, I will likely need to spend a few minutes figuring
out what you’re trying to do.Very hard to do standing up and
looking over your shoulder.
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 ( !! ) [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
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
Play and Share

We do in class one day



On the following day




Put your name in comments at top
Save in a file on your Z drive
Everyone brings up their file
You review other students’ solutions
I will walk around and give a checkmark for participation to any
student who has a text file on screen with really good effort… doing
2 of 8, for example, would not be good effort
Why?

Haskell is kinda weird… the only way to get used to it is to work
with it. I really struggled at first, expect some of you will as well. Play
& Share are small warmups that should help you get comfortable
with Haskell and do labs. Worth the effort!
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.