CSc 372 - Comparative Programming Languages
4 : Haskell -- Basics

Christian Collberg

Department of Computer Science

University of Arizona

1 The Hugs Interpreter

2 The Hugs Interpreter...


Haskell Types


3 Expressions

4 Haskell Types

5 Type inference

6 Type inference...


Simple Types


7 Int

8 Int -- Operators

 Op Precedence Associativity Description
 ^ 8 right Exponentiation
 *, / 7 left Mul, Div
 `div` 7 free Division
 `rem` 7 free Remainder
 `mod` 7 free Modulus
 +, - 6 left Add, Subtract
 ==,/= 4 free (In-) Equality
 <,<=,>,>= 4 free Relational Comparison

9 Int...


\begin{program}
\redtt{> 4*12-6} \\
42\\
\redtt{> 126 \lq div\lq  3} \\
42 \\
\redtt{> div 126 3} \\
42
\end{program}

10 Int...


\begin{xprogram}
1+2-3 \xxxxx $\Rightarrow$ (1+2)-3 \\
1+2*3 \xxxxx $\Rightarr...
...
2\verb+^+3\verb+^+4 \xxxxx $\Rightarrow$ 2\verb+^+(3\verb+^+4)
\end{xprogram}


\begin{xprogram}
4==5==6 \xxxxx $\Rightarrow$ \redtxt{ERROR} \\
12/6/3 \xxxxx ...
...\redtxt{0.666666666666667} \\
12/(6/3) \xxxxx $\Rightarrow$ 6.0
\end{xprogram}

11 Integer


\begin{program}
\redtt{> (3333333 :: Integer) * (44444444444444 :: Integer)} \\
148148133333331851852
\end{program}


\begin{program}
\redtt{> 2\^{}64} \\
18446744073709551616
\end{program}

12 Integer...


\begin{program}
\redtt{> (3333333 :: Integer) * (44 :: Int)} \\
ERROR - Type error in application
\end{program}

\begin{program}
\redtt{> (toInteger (55 :: Int)) * (66 :: Integer)} \\
3630
\end{program}

13 Float and Double

14 Char

15 Char -- Built-in Functions


\begin{gprogram}
ord :: Char -> Int \\
char :: Int -> Char \\
toUpper, toLower...
...s$ :: Char -> Bool \\
isUpper,isLower,$\cdots$ :: Char -> Bool
\end{gprogram}


\begin{gprogram}
ord 'a' $\Rightarrow$ 97 \xxxxx toUpper 'a' $\Rightarrow$ 'A'...
...chr 65 $\Rightarrow$ 'A' \xxxxx isDigit 'a' $\Rightarrow$ False
\end{gprogram}

16 String

17 Bool

Op Precedence Associativity Description
&& 3 right logical and
|| 2 right logical or
not 9 - logical not


\begin{gprogram}
3 < 5 \&\& 4 > 2 \xxxxxxxx $\Leftrightarrow$ (3 < 5) \&\& (4 >...
...xxxx $\Leftrightarrow$
True \verb+\vert\vert+ (False \&\& True)
\end{gprogram}


Haskell Functions


18 Functions

19 Functions...


List and Tuple Types


20 Lists


\begin{program}
\redtt{> [1,2,3]} \\
{[1,2,3]}\\
\redtt{> [True,False] :: [Boo...
...\
\redtt{> [1,True]} \\
ERROR \\
\redtt{> length [1,2,3]} \\
3
\end{program}

21 Tuples

Examples:
\begin{gprogram}
type Complex = (Float,Float) \\
mkComplex :: Float -> Float -> Complex \\
mkComplex re im = (re, im)
\end{gprogram}

22 Tuples...


\begin{gprogram}
type Complex = (Float,Float) \\
mkComplex :: Float -> Float ->...
...
addComplex (mkComplex 5 3) (mkComplex 4 2) $\Rightarrow$ (9,5)
\end{gprogram}


Haskell Scripts


23 Editing and Loading Scripts

24 The Offside Rule


\begin{gprogram}
square x = \xxxx x * x \\
\xxxx +2 \\
cube x = $\cdots$
\end{gprogram}

25 The Offside Rule...

OpenBox

CloseBox

26 Comments


Editing Scripts


27 Emacs

28 Readings and References

29 Summary

30 Homework

  1. Start Hugs.
  2. Enter the commaint function and try it out.
  3. Enter the addComplex and mkComplex functions and try them out.
  4. Try the standard functions fst x and snd x on complex values. What do fst and snd do?
  5. Try out the Eliza application in /usr/local/hugs98/lib/hugs/demos/Eliza.hs on lectura.

31 Homework...


\begin{gprogram}
\redtxt{? isAlphaNum 'a'} \\
\x True \\
\redtxt{? isAlphaNum ...
...Num ';'} \\
\x False \\
\redtxt{? isAlphaNum '@'} \\
\x False
\end{gprogram}

32 Homework...


\begin{gprogram}
eOr :: Bool -> Bool -> Bool \\
eOr x y = $\cdots$ \\
\\
\re...
...lse True} \\
\x True \\
\redtxt{? eOr False False} \\
\x False
\end{gprogram}

33 Homework...


\begin{gprogram}
charToInt :: Char -> Int \\
charToInt c = $\cdots$ \\
\\
\r...
...t{? charToInt '0'} \\
\x 0 \\
\redtxt{? charToInt 'y'} \\
\x 0
\end{gprogram}



Christian S. Collberg
2007-08-23