real world haskell: lecture 1
out of 48
Post on 29-Aug-2014
Embed Size (px)
- Real World Haskell: Lecture 1 Bryan OSullivan 2009-10-07
- Welcome! A few things to mull over: Our pace will be fairly rapid. Stop me and ask questionsearly and often. I assume no prior Haskell or functional programming exposure.
- Whats software, doc? What does a program do? It consumes old data. It computes over the old data. It produces new data.
- A familiar program Consider the Unix sort command: Consumes (possibly unsorted) lines of text. Sorts the lines. Produces sorted lines of text.
- Have you ever run sort? If so, youre already a functional programmer.
- Whats functional programming? Denition Functional programming is a style of programming that emphasizes functions and function application.
- Whats a function? Denition A function inspects its input, and produces an output. Denition Function application involves supplying a function with one or more arguments (inputs).
- Why is the sort command functional? Discuss.
- So what? I can already write code that inspects inputs and produces outputs in C, Python, or whatever. So whats dierent about functional programming (FP)? We only inspect inputs; we dont change them. In fact, we generally dont change any data.
- Whats interesting about functional programming? Ill tell you what got me hooked: It requires a substantially dierent way of thinking about programming. In exchange for the challenge, it oers great expressiveness and conciseness. It is beautiful.
- Why Haskell? Why choose Haskell over Erlang, Clojure, F#, OCaml, . . . ? It is the most radical of the practical functional languages. It will force you to learn many new things at once, and to unlearn things that perhaps you thought you knew.
- Getting started with Haskell Well be using the Haskell Platform: http://hackage.haskell.org/platform/ What is this Platform, anyway? A modern, optimising compiler (ghc) An interactive interpreter (ghci) Useful standard libraries An awesome package manager (cabal)
- Starting with interaction The interactive interpreter for the Haskell Platform is named ghci (for GHC interactive). When you run it, youll see something like this: GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> That Prelude> is the prompt.
- Really simple stu What happens when you enter these expressions at the ghci prompt? 2+2 putStrLn hi mom! foo ++ bar :quit
- Handy things to know You should be able to use your arrow keys to edit your ghci input. Up and down arrows should go through your input history. Tab should complete names: try typing put.
- Haskell source les The standard Haskell source le extension is .hs. By convention, les use InterCapsNaming. Save the following in a le named Hello.hs: main = putStrLn h e l l o , w o r l d !
- Run your program immediately We can run our new program straight away, via the runghc batch interpreter: $ runghc Hello.hs hello, world!
- Compilation We can also compile our new program to a native executable: $ ghc --make Hello [1 of 1] Compiling Main ( Hello.hs, Hello.o ) Linking Hello ... (GHCs --make option is super-useful!) We can run our new program in the usual way: $ ./Hello hello, world!
- Operators and things Haskells rules for writing expressions with operators should look familiar. b 2 4 a c The operator is (integral) exponentiation. Expressions group as you might expect, i.e. (b2) (4ac). Confused about operator precedence? Parens are your friend!
- What about functions? To apply a function to some arguments, we simply write it next to its arguments. length hello Function application binds tighter than operations, so if you write this: sqrt 2 3 What youll get is this: (sqrt 2) 3 If you mean something else, tell the compiler! sqrt (2 3)
- A more complex expression Look familiar? (b + sqrt (b2 4ac)) / (2a) (Its part of the solution to a quadratic equation.)
- Expressions are all very well But we need a bit more to do anything useful. We have to be able to give things names. To dene something, we supply a name, an equals sign, and an expression. e = 2.718281828459045
- A more useful denition When we supply more than one name before the equals sign, the remainder are the arguments to a function. oneRoot a b c = (b + ( b 2 + 4 a c ) ) / ( 2 a ) Weve now dened a function named oneRoot, with three arguments. Try saving this denition into a source le, then load it into ghci and use it. Whats the result of this expression in ghci? oneRoot 2 3 4
- Your turn! Im going to open up an Emacs window. Tell me how to write a function that computes the mean of three numbers.
- Speaking of Emacs What are the best tools for working with Haskell source code? Emacs vim Google for emacs haskell mode or vim haskell mode for details. Do you prefer IDEs? At least for now, youve come to the wrong language. Back to Visual Studio with you! Less ippant answer: there are no mature Haskell IDEs yet.
- Of source les and editors Haskellers hate tab characters! (For the same reason Python programmers do: theyre unportable.) A decent Haskell mode for a good editor will give you some useful features: Syntax highlighting. Intelligent indentation. Compilation and jump-to-error support. No tabs! Maybe interaction with ghci.
- A very tiny, marginally useful program wordCount x s = show ( l e n g t h ( words x s ) ) ++ n main = i n t e r a c t wordCount Whats going on here? The words function breaks up a text string into a list of words. The length function. . . well, you can guess. The show function takes something (in this case, a number) and represents it as a string. The ++ operator means append.
- Oh, and then theres interact The interact function is your gateway to Unix shell pipeline bliss. It: reads from standard input; feeds it to your function as a string; consumes the string that your function produces; and prints it to standard output.
- Another very tiny program I need your help! Who can suggest whats going on here? import Data . L i s t ( s o r t ) s o r t L i n e s xs = unlines ( sort ( l i n e s xs )) main = i n t e r a c t s o r t L i n e s
- Figuring stu out When youre in exploratory mode (i.e. most of the time, at least for me), ghci is your friend. Wonder what lines does? Simply try it, and nd out! Prelude> lines "foonbarnquuxn" ["foo","bar","quux"] What else did we nd out here?
- Lists The syntactic sugar for a list of items is square brackets surrounding items, which are separated by commas: [4 ,8 ,15 ,16 ,23 ,42] [ True , F a l s e ] [ L i n d e n , Lab ] Lets try some experiments with lists. Can we write this? [7, True] What about this? [[1,2],[3,4]]
- More experimentation What does the unlines function do? What about sort?
- Another small program Lets develop something! Another Unix-like
View more >