Real World Haskell: Lecture 1

Download Real World Haskell: Lecture 1

Post on 29-Aug-2014

3.602 views

Category:

Education

2 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> Real World Haskell: Lecture 1 Bryan OSullivan 2009-10-07 </li> <li> 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. </li> <li> Whats software, doc? What does a program do? It consumes old data. It computes over the old data. It produces new data. </li> <li> A familiar program Consider the Unix sort command: Consumes (possibly unsorted) lines of text. Sorts the lines. Produces sorted lines of text. </li> <li> Have you ever run sort? If so, youre already a functional programmer. </li> <li> Whats functional programming? Denition Functional programming is a style of programming that emphasizes functions and function application. </li> <li> 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). </li> <li> Why is the sort command functional? Discuss. </li> <li> 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. </li> <li> 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. </li> <li> 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. </li> <li> 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) </li> <li> 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&gt; That Prelude&gt; is the prompt. </li> <li> Really simple stu What happens when you enter these expressions at the ghci prompt? 2+2 putStrLn hi mom! foo ++ bar :quit </li> <li> 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. </li> <li> 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 ! </li> <li> Run your program immediately We can run our new program straight away, via the runghc batch interpreter: $ runghc Hello.hs hello, world! </li> <li> 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! </li> <li> 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! </li> <li> 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) </li> <li> A more complex expression Look familiar? (b + sqrt (b2 4ac)) / (2a) (Its part of the solution to a quadratic equation.) </li> <li> 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 </li> <li> 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 </li> <li> Your turn! Im going to open up an Emacs window. Tell me how to write a function that computes the mean of three numbers. </li> <li> 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. </li> <li> 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. </li> <li> 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. </li> <li> 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. </li> <li> 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 </li> <li> 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&gt; lines "foonbarnquuxn" ["foo","bar","quux"] What else did we nd out here? </li> <li> 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]] </li> <li> More experimentation What does the unlines function do? What about sort? </li> <li> Another small program Lets develop something! Another Unix-like command! Well write a program that prexes each line of input with a line number. Okay. . . so now what? </li> <li> When in doubt If I dont know how to solve a problem, I like to start by seeing if there are bits of it I know how to solve. What do we already know about? Dening new functions! And, of course, using existing functions! We have a small library of built-in functions and operators, e.g. show, unlines, and ++. </li> <li> The small problem Given one number and one line of text, how should we render them? </li> <li> The small problem Given one number and one line of text, how should we render them? oneNumber n s = show n ++ : ++ s </li> <li> The small problem Given one number and one line of text, how should we render them? oneNumber n s = show n ++ : ++ s Lets try this function in ghci: Prelude&gt; oneNumber 3 "blargh" "3: blargh" </li> <li> Powering up Thats a good start. But now what? We need to produce a whole series of lines with numbers. </li> <li> Powering up Thats a good start. But now what? We need to produce a whole series of lines with numbers. lineNumbers n xs = i f x s == [ ] then [] else oneNumber n ( head x s ) : l i n e N u m b e r s ( n+1) ( t a i l x s ) </li> <li> Powering up Thats a good start. But now what? We need to produce a whole series of lines with numbers. lineNumbers n xs = i f x s == [ ] then [] else oneNumber n ( head x s ) : l i n e N u m b e r s ( n+1) ( t a i l x s ) Hey! See that little lonely :? Its a list constructor (like cons in Lisp or Scheme). </li> <li> The quantum leap Wow, we just did something interesting: We wrote a recursive function. Not bad for the rst lecture! </li> <li> The quantum leap Wow, we just did something interesting: We wrote a recursive function. Not bad for the rst lecture! We still lack a little glue to create a complete program. </li> <li> The quantum leap Wow, we just did something interesting: We wrote a recursive function. Not bad for the rst lecture! We still lack a little glue to create a complete program. b u n c h i e x s = unwords ( l i n e N u m b e r s 1 ( l i n e s x s ) ) main = i n t e r a c t b u n c h i e </li> <li> Accompanying materials Where should you be going to learn more? http://book.realworldhaskell.org/ http://learnyouahaskell.com/ http://haskell.org/ http://slideshare.net/bos31337 #haskell on irc.freenode.net (Im bos) </li> <li> Accompanying materials Where should you be going to learn more? http://book.realworldhaskell.org/ http://learnyouahaskell.com/ http://haskell.org/ http://slideshare.net/bos31337 #haskell on irc.freenode.net (Im bos) Source code for these slides and examples: darcs get http://darcs.serpentine.com/rwh-course </li> <li> Recap What have we covered today? What functional programming is. A tiny bit about Haskell. The Haskell Platform, and why it matters. Writing expressions, and dening functions. Simple interactive Unix-like commands. </li> <li> Recap What have we covered today? What functional programming is. A tiny bit about Haskell. The Haskell Platform, and why it matters. Writing expressions, and dening functions. Simple interactive Unix-like commands. There will be homework. Assignments will go out later today to the haskell mailing list. </li> <li> Recap What have we covered today? What functional programming is. A tiny bit about Haskell. The Haskell Platform, and why it matters. Writing expressions, and dening functions. Simple interactive Unix-like commands. There will be homework. Assignments will go out later today to the haskell mailing list. Thanks! Questions? </li> </ul>