CSc 372 - Comparative Programming Languages
3 : Haskell -- Introduction

Christian Collberg

Department of Computer Science

University of Arizona

1 What is Haskell?

2 What is Haskell?...

ReadEvalPrint


\begin{gprogram}
\redtxt{> hugs} \\
Prelude> (2*5)+3 \\
13
\end{gprogram}

3 What is Haskell?...


\begin{gprogram}
\redtt{> hugs} \\
Prelude> :load /usr/lib64/hugs/demos/Eliza.h...
... \redtt{i'm bored!} \\
Did you come to me because you are bored?
\end{gprogram}

4 What is Haskell?...

eliza = interact (writeStr hi $ session initial [])
 where hi = "\n\
             \Hi! I'm Eliza. I am your personal therapy computer.\n\
             \Please tell me your problem.\n\
             \\n"
 
session rs prev
       = readLine "> " (\l ->
         let ws             = words (trim l)
             (response,rs') = if prev==ws then repeated rs else answer rs ws
         in  writeStr (response ++ "\n\n") $ session rs' ws)

5 commaint - A Haskell Program

6 commaint - A Haskell Program...

From the commaint documentation:

[commaint] takes a single string argument containing a sequence of digits, and outputs the same sequence with commas inserted after every group of three digits, $\cdots$

7 commaint - A Haskell Program...

Sample interaction:
\begin{gprogram}
\redtxt{? commaint ''1234567''} \\
\x 1,234,567
\end{gprogram}

commaint in Haskell:
\begin{gprogram}
commaint = \= reverse . foldr1 ($\backslash$x y->x++'',''++y) ....
...= takeWhile (not.null) . \\
\x \x map (take n).iterate (drop n)
\end{gprogram}

8 commaint - A Haskell Program...

CommaInt

9 commaint - A Haskell Program...

commaint in Haskell:
\begin{gprogram}
commaint = \= reverse . foldr1 ($\backslash$x y->x++'',''++y) ....
...= takeWhile (not.null) . \\
\x \x map (take n).iterate (drop n)
\end{gprogram}

commaint in English:

``First reverse the input string. Take the resulting string and separate into chunks of length 3. Then append the chunks together, inserting a comma between chunks. Reverse the resulting string.''

10 commaint - A Haskell Program...


\begin{gprogram}
commaint = \= reverse . foldr1 ($\backslash$x y->x++'',''++y) ....
...= takeWhile (not.null) . \\
\x \x map (take n).iterate (drop n)
\end{gprogram}

11 commaint - A Haskell Program...


\begin{gprogram}
commaint = \= reverse . foldr1 ($\backslash$x y->x++'',''++y) ....
...= takeWhile (not.null) . \\
\x \x map (take n).iterate (drop n)
\end{gprogram}

12 commaint - A Haskell Program...


\begin{gprogram}
commaint = \= reverse . foldr1 ($\backslash$x y->x++'',''++y) ....
...= takeWhile (not.null) . \\
\x \x map (take n).iterate (drop n)
\end{gprogram}

13 commaint - A Haskell Program...


\begin{gprogram}
commaint = \= reverse . foldr1 ($\backslash$x y->x++'',''++y) ....
...= takeWhile (not.null) . \\
\x \x map (take n).iterate (drop n)
\end{gprogram}

14 commaint - A Haskell Program...


\begin{gprogram}
\redtxt{? reverse ''1234567''} \\
\x 7654321\\
\redtxt{? take...
... [''7654321'', ''4321'', ''1'', [], [], $\cdots$ \{interrupt!\}
\end{gprogram}



Christian S. Collberg
2007-08-21