SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
Real World Haskell:
     Lecture 1

   Bryan O’Sullivan


     2009-10-07
Welcome!




  A few things to mull over:
      Our pace will be fairly rapid.
      Stop me and ask questions—early and often.
      I assume no prior Haskell or functional programming exposure.
What’s 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, you’re already a functional programmer.
What’s functional programming?




   Definition
   Functional programming is a style of programming that emphasizes
   functions and function application.
What’s a function?




   Definition
   A function inspects its input, and produces an output.

   Definition
   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 what’s different about functional programming (FP)?
      We only inspect inputs; we don’t change them.
      In fact, we generally don’t change any data.
What’s interesting about functional programming?




   I’ll tell you what got me hooked:
       It requires a substantially different way of thinking about
       programming.
       In exchange for the challenge, it offers 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



   We’ll 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, you’ll 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 stuff




   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<TAB>.
Haskell source files




       The standard Haskell source file extension is “.hs”.
       By convention, files use InterCapsNaming.
       Save the following in a file 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 ...
      (GHC’s “--make” option is super-useful!)
      We can run our new program in the usual way:
      $ ./Hello
      hello, world!
Operators and things



   Haskell’s 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. (bˆ2) − (4∗a∗c).
   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 you’ll 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 (bˆ2 ∗ 4∗a∗c)) / (2∗a)
   (It’s 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 define something, we supply a name, an equals sign, and an
   expression.
   e = 2.718281828459045
A more useful definition


   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 )
   We’ve now defined a function named oneRoot, with three
   arguments.
       Try saving this definition into a source file, then load it into
       ghci and use it.
       What’s the result of this expression in ghci?
       oneRoot 2 3 4
Your turn!




   I’m 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, you’ve come to the wrong language. Back to
       Visual Studio with you!
       Less flippant answer: there are no mature Haskell IDEs yet.
Of source files and editors


   Haskellers hate tab characters! (For the same reason Python
   programmers do: they’re 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

   What’s 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 there’s 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 what’s 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 stuff out



   When you’re 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 find out!

   Prelude> lines "foonbarnquuxn"
   ["foo","bar","quux"]

   What else did we find 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 ” ]

   Let’s 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




   Let’s develop something! Another Unix-like command!

   We’ll write a program that prefixes each line of input with a line
   number.

   Okay. . . so now what?
When in doubt



  If I don’t 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?
      Defining new functions!
      And, of course, using existing functions!
      We have a small library of built-in functions and operators,
      e.g. show, unlines, and ++.
The small problem



   Given one number and one line of text, how should we render
   them?
The small problem



   Given one number and one line of text, how should we render
   them?

   oneNumber n s = show n ++ ” : ” ++ s
The small problem



   Given one number and one line of text, how should we render
   them?

   oneNumber n s = show n ++ ” : ” ++ s

   Let’s try this function in ghci:

   Prelude> oneNumber 3 "blargh"
   "3: blargh"
Powering up


   That’s a good start. But now what?

   We need to produce a whole series of lines with numbers.
Powering up


   That’s 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 )
Powering up


   That’s 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 “:”? It’s a list constructor (like cons in
   Lisp or Scheme).
The quantum leap


  Wow, we just did something interesting:
      We wrote a recursive function.
  Not bad for the first lecture!
The quantum leap


  Wow, we just did something interesting:
      We wrote a recursive function.
  Not bad for the first lecture!

  We still lack a little glue to create a complete program.
The quantum leap


  Wow, we just did something interesting:
        We wrote a recursive function.
  Not bad for the first 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
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 (I’m bos)
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 (I’m bos)

   Source code for these slides and examples:
       darcs get http://darcs.serpentine.com/rwh-course
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 defining functions.
        Simple interactive Unix-like commands.
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 defining functions.
        Simple interactive Unix-like commands.


  There will be homework. Assignments will go out later today to
  the haskell mailing list.
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 defining functions.
        Simple interactive Unix-like commands.


  There will be homework. Assignments will go out later today to
  the haskell mailing list.

  Thanks! Questions?

Más contenido relacionado

La actualidad más candente

Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data scienceJohn Cant
 
Scala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data TypeScala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data TypePhilip Schwarz
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...Philip Schwarz
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldPhilip Schwarz
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Philip Schwarz
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 

La actualidad más candente (20)

Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Scala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data TypeScala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data Type
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 

Destacado

Fotos graciosas 33657
Fotos graciosas 33657Fotos graciosas 33657
Fotos graciosas 33657serviojapon
 
Learn Haskell The Easy Way
Learn Haskell The Easy WayLearn Haskell The Easy Way
Learn Haskell The Easy WayYC Ling
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real Worldosfameron
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
People Service Demo 01
People Service Demo 01People Service Demo 01
People Service Demo 01guest5b24c0
 
Top Transfers
Top TransfersTop Transfers
Top TransfersSameer
 
Joies d'Eivissa
Joies d'EivissaJoies d'Eivissa
Joies d'EivissaGemma Tur
 
7th Grade Orientation 2008
7th Grade Orientation 20087th Grade Orientation 2008
7th Grade Orientation 2008guest872e67
 
From OER to Open Education: Perceptions of student teachers after creating di...
From OER to Open Education: Perceptions of student teachers after creating di...From OER to Open Education: Perceptions of student teachers after creating di...
From OER to Open Education: Perceptions of student teachers after creating di...Gemma Tur
 
POSK-AP-B-I
POSK-AP-B-IPOSK-AP-B-I
POSK-AP-B-IEwaB
 
Origens i consolidació del catalanisme
Origens i consolidació del catalanismeOrigens i consolidació del catalanisme
Origens i consolidació del catalanismeGemma Ajenjo Rodriguez
 
Capturas De Site Meter
Capturas De Site MeterCapturas De Site Meter
Capturas De Site Meterguest6359fd
 
10 business models that rocked in 2010
10 business models that rocked in 201010 business models that rocked in 2010
10 business models that rocked in 2010supermanchander
 
Konferencja PK Koszalin 2008 - Długi ogon wyszukiwania
Konferencja PK Koszalin 2008 - Długi ogon wyszukiwaniaKonferencja PK Koszalin 2008 - Długi ogon wyszukiwania
Konferencja PK Koszalin 2008 - Długi ogon wyszukiwaniaEwaB
 

Destacado (20)

El Protocolo de Kioto
El Protocolo de KiotoEl Protocolo de Kioto
El Protocolo de Kioto
 
Fotos graciosas 33657
Fotos graciosas 33657Fotos graciosas 33657
Fotos graciosas 33657
 
Learn Haskell The Easy Way
Learn Haskell The Easy WayLearn Haskell The Easy Way
Learn Haskell The Easy Way
 
Haskell study 8
Haskell study 8Haskell study 8
Haskell study 8
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Liberalisme i nacionalisme I
Liberalisme i nacionalisme ILiberalisme i nacionalisme I
Liberalisme i nacionalisme I
 
People Service Demo 01
People Service Demo 01People Service Demo 01
People Service Demo 01
 
Top Transfers
Top TransfersTop Transfers
Top Transfers
 
Joies d'Eivissa
Joies d'EivissaJoies d'Eivissa
Joies d'Eivissa
 
7th Grade Orientation 2008
7th Grade Orientation 20087th Grade Orientation 2008
7th Grade Orientation 2008
 
From OER to Open Education: Perceptions of student teachers after creating di...
From OER to Open Education: Perceptions of student teachers after creating di...From OER to Open Education: Perceptions of student teachers after creating di...
From OER to Open Education: Perceptions of student teachers after creating di...
 
Pronk like you mean it
Pronk like you mean itPronk like you mean it
Pronk like you mean it
 
POSK-AP-B-I
POSK-AP-B-IPOSK-AP-B-I
POSK-AP-B-I
 
Origens i consolidació del catalanisme
Origens i consolidació del catalanismeOrigens i consolidació del catalanisme
Origens i consolidació del catalanisme
 
Mjedi101109
Mjedi101109Mjedi101109
Mjedi101109
 
Do And Does
Do And DoesDo And Does
Do And Does
 
Capturas De Site Meter
Capturas De Site MeterCapturas De Site Meter
Capturas De Site Meter
 
10 business models that rocked in 2010
10 business models that rocked in 201010 business models that rocked in 2010
10 business models that rocked in 2010
 
Konferencja PK Koszalin 2008 - Długi ogon wyszukiwania
Konferencja PK Koszalin 2008 - Długi ogon wyszukiwaniaKonferencja PK Koszalin 2008 - Długi ogon wyszukiwania
Konferencja PK Koszalin 2008 - Długi ogon wyszukiwania
 

Similar a Real World Haskell lecture introduces functional programming concepts

Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospectivechenge2k
 
Hasktut
HasktutHasktut
Hasktutkv33
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingLex Sheehan
 
Playfulness at Work
Playfulness at WorkPlayfulness at Work
Playfulness at WorkErin Dees
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distributionRaymond Tay
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifestohu hans
 
Functional Programming in Excel
Functional Programming in ExcelFunctional Programming in Excel
Functional Programming in ExcelFelienne Hermans
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.10 book - Part 49 of 212The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.10 book - Part 49 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.5.3 book - Part 39 of 184The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.5.3 book - Part 39 of 184Mahmoud Samir Fayed
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsRuth Marvin
 
The Ring programming language version 1.4 book - Part 11 of 30
The Ring programming language version 1.4 book - Part 11 of 30The Ring programming language version 1.4 book - Part 11 of 30
The Ring programming language version 1.4 book - Part 11 of 30Mahmoud Samir Fayed
 
Functional programming (Let's fall back in love with Programming)
Functional programming (Let's fall back in love with Programming)Functional programming (Let's fall back in love with Programming)
Functional programming (Let's fall back in love with Programming)Sudipta Mukherjee
 

Similar a Real World Haskell lecture introduces functional programming concepts (20)

Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
 
Hasktut
HasktutHasktut
Hasktut
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional Programming
 
Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Playfulness at Work
Playfulness at WorkPlayfulness at Work
Playfulness at Work
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distribution
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifesto
 
Functional Programming in Excel
Functional Programming in ExcelFunctional Programming in Excel
Functional Programming in Excel
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.10 book - Part 49 of 212The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.10 book - Part 49 of 212
 
The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.5.3 book - Part 39 of 184The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.5.3 book - Part 39 of 184
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
The Ring programming language version 1.4 book - Part 11 of 30
The Ring programming language version 1.4 book - Part 11 of 30The Ring programming language version 1.4 book - Part 11 of 30
The Ring programming language version 1.4 book - Part 11 of 30
 
Functional programming (Let's fall back in love with Programming)
Functional programming (Let's fall back in love with Programming)Functional programming (Let's fall back in love with Programming)
Functional programming (Let's fall back in love with Programming)
 
Beginning Python
Beginning PythonBeginning Python
Beginning Python
 

Último

week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 

Último (20)

week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 

Real World Haskell lecture introduces functional programming concepts

  • 1. Real World Haskell: Lecture 1 Bryan O’Sullivan 2009-10-07
  • 2. Welcome! A few things to mull over: Our pace will be fairly rapid. Stop me and ask questions—early and often. I assume no prior Haskell or functional programming exposure.
  • 3. What’s software, doc? What does a program do? It consumes old data. It computes over the old data. It produces new data.
  • 4. A familiar program Consider the Unix sort command: Consumes (possibly unsorted) lines of text. Sorts the lines. Produces sorted lines of text.
  • 5. Have you ever run sort? If so, you’re already a functional programmer.
  • 6. What’s functional programming? Definition Functional programming is a style of programming that emphasizes functions and function application.
  • 7. What’s a function? Definition A function inspects its input, and produces an output. Definition Function application involves supplying a function with one or more arguments (inputs).
  • 8. Why is the sort command functional? Discuss.
  • 9. So what? I can already write code that inspects inputs and produces outputs in C, Python, or whatever. So what’s different about functional programming (FP)? We only inspect inputs; we don’t change them. In fact, we generally don’t change any data.
  • 10. What’s interesting about functional programming? I’ll tell you what got me hooked: It requires a substantially different way of thinking about programming. In exchange for the challenge, it offers great expressiveness and conciseness. It is beautiful.
  • 11. 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.
  • 12. Getting started with Haskell We’ll 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)
  • 13. Starting with interaction The interactive interpreter for the Haskell Platform is named ghci (for “GHC interactive”). When you run it, you’ll 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.
  • 14. Really simple stuff What happens when you enter these expressions at the ghci prompt? 2+2 putStrLn ”hi mom!” ”foo” ++ ”bar” :quit
  • 15. 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<TAB>.
  • 16. Haskell source files The standard Haskell source file extension is “.hs”. By convention, files use InterCapsNaming. Save the following in a file named Hello.hs: main = putStrLn ” h e l l o , w o r l d ! ”
  • 17. Run your program immediately We can run our new program straight away, via the runghc batch interpreter: $ runghc Hello.hs hello, world!
  • 18. 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 ... (GHC’s “--make” option is super-useful!) We can run our new program in the usual way: $ ./Hello hello, world!
  • 19. Operators and things Haskell’s 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. (bˆ2) − (4∗a∗c). Confused about operator precedence? Parens are your friend!
  • 20. 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 you’ll get is this: (sqrt 2) − 3 If you mean something else, tell the compiler! sqrt (2 − 3)
  • 21. A more complex expression Look familiar? (−b + sqrt (bˆ2 ∗ 4∗a∗c)) / (2∗a) (It’s part of the solution to a quadratic equation.)
  • 22. 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 define something, we supply a name, an equals sign, and an expression. e = 2.718281828459045
  • 23. A more useful definition 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 ) We’ve now defined a function named oneRoot, with three arguments. Try saving this definition into a source file, then load it into ghci and use it. What’s the result of this expression in ghci? oneRoot 2 3 4
  • 24. Your turn! I’m going to open up an Emacs window. Tell me how to write a function that computes the mean of three numbers.
  • 25. 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, you’ve come to the wrong language. Back to Visual Studio with you! Less flippant answer: there are no mature Haskell IDEs yet.
  • 26. Of source files and editors Haskellers hate tab characters! (For the same reason Python programmers do: they’re 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.
  • 27. 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 What’s 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.”
  • 28. Oh, and then there’s 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.
  • 29. Another very tiny program I need your help! Who can suggest what’s 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
  • 30. Figuring stuff out When you’re 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 find out! Prelude> lines "foonbarnquuxn" ["foo","bar","quux"] What else did we find out here?
  • 31. 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 ” ] Let’s try some experiments with lists. Can we write this? [7, True] What about this? [[1,2],[3,4]]
  • 32. More experimentation What does the unlines function do? What about sort?
  • 33. Another small program Let’s develop something! Another Unix-like command! We’ll write a program that prefixes each line of input with a line number. Okay. . . so now what?
  • 34. When in doubt If I don’t 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? Defining new functions! And, of course, using existing functions! We have a small library of built-in functions and operators, e.g. show, unlines, and ++.
  • 35. The small problem Given one number and one line of text, how should we render them?
  • 36. The small problem Given one number and one line of text, how should we render them? oneNumber n s = show n ++ ” : ” ++ s
  • 37. The small problem Given one number and one line of text, how should we render them? oneNumber n s = show n ++ ” : ” ++ s Let’s try this function in ghci: Prelude> oneNumber 3 "blargh" "3: blargh"
  • 38. Powering up That’s a good start. But now what? We need to produce a whole series of lines with numbers.
  • 39. Powering up That’s 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 )
  • 40. Powering up That’s 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 “:”? It’s a list constructor (like cons in Lisp or Scheme).
  • 41. The quantum leap Wow, we just did something interesting: We wrote a recursive function. Not bad for the first lecture!
  • 42. The quantum leap Wow, we just did something interesting: We wrote a recursive function. Not bad for the first lecture! We still lack a little glue to create a complete program.
  • 43. The quantum leap Wow, we just did something interesting: We wrote a recursive function. Not bad for the first 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
  • 44. 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 (I’m bos)
  • 45. 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 (I’m bos) Source code for these slides and examples: darcs get http://darcs.serpentine.com/rwh-course
  • 46. 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 defining functions. Simple interactive Unix-like commands.
  • 47. 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 defining functions. Simple interactive Unix-like commands. There will be homework. Assignments will go out later today to the haskell mailing list.
  • 48. 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 defining functions. Simple interactive Unix-like commands. There will be homework. Assignments will go out later today to the haskell mailing list. Thanks! Questions?