09 Haskell Part 1

Programming Languages Haskell Part 1 Dr. Philip Cannata 1 A programming language is a language with a welldefined sy...

0 downloads 147 Views 149KB Size
Programming Languages Haskell Part 1

Dr. Philip Cannata

1

A programming language is a language with a welldefined syntax (lexicon and grammar), type system, and semantics that can be used to implement a set of algorithms. Haskell

Dr. Philip Cannata

2

Haskell: /lusr/bin/hugs, should be in your default $PATH or for windows, download and install winhugs at http://cvs.haskell.org/Hugs/pages/downloading.htm $ hugs __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Bugs: http://hackage.haskell.org/trac/hugs || || Version: 20051031 _________________________________________ Haskell 98 mode: Restart with command line option -98 to enable extensions Type :? for help Hugs> :load 09H1 Main>

Dr. Philip Cannata

To load the file “09H1.hs” from the directory in which you started hugs 3

Haskell Expression

Dr. Philip Cannata

Hugs> 2 * 4 ^2 Number Expressions 32 Hugs> 8^2 64 Hugs> (2 * 4) ^ 2 64 Hugs> 2 + 4 ^ 2 18 Hugs> 2 ^ 200 1606938044258990275541962092341162602522202993782792835301376 Hugs> True && False Boolean Expressions False Hugs> True || False True Hugs> 3 < 5 True Hugs> 'c' < 'p' Character Expressions True Hugs> 'c' > 'p' False Hugs> "tree" < "rock" String Expressions False Hugs> "tree" > "rock" True 4

Haskell Basic Data Structures

Hugs> ("dog", "cat", 5) ("dog","cat",5)

Tuples

Hugs> ["dog", "cat", 5] ERROR - Cannot infer instance *** Instance : Num [Char] *** Expression : ["dog","cat",5]

Lists

Hugs> ["dog", "cat", "5"] ["dog","cat","5"] Hugs> 1 : [] [1]

List Construction

Hugs> 1 : [2,3,4] [1,2,3,4] [expression | generator] Hugs> [2 * x ^ 2 | x <- [1, 2, 3 ,4]] [2,8,18,32]

List Comprehension

Hugs> [x * y | (x, y) <- [(1, 2), (3, 4), (5, 6)]] [2,12,30] Dr. Philip Cannata

5

Haskell Basic Data Structures continued

List Comprehension Cross product Hugs> [(x, y) | x <- [1, 3 .. 6], y <- ['a', 'b', 'c', 'd']] [(1,'a'),(1,'b'),(1,'c'),(1,'d'),(3,'a'),(3,'b'),(3,'c'),(3,'d'),(5,'a'),(5,'b'),(5,'c'),(5,'d')] Hugs> [(x, y) | x <- [1..4], y <- [1..4]] [(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4),(3,1),(3,2),(3,3),(3,4),(4,1),(4,2), (4,3),(4,4)] “<“ and “>” Relation Hugs> [(x, y) | x <- [0..4], y <- [0..4], x < y] [(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] Hugs> [(x, y) | x <- [0..4], y <- [0..4], x > y] [(1,0),(2,0),(2,1),(3,0),(3,1),(3,2),(4,0),(4,1),(4,2),(4,3)] Hugs> [x | x <- [1..100], y <- [1..100], 72 == x * y] [1,2,3,4,6,8,9,12,18,24,36,72]

Dr. Philip Cannata

Factors of 72

6

Haskell Basic Data Structures continued

Dr. Philip Cannata

List Comprehension Database Query Main> [empno | (empno, _, _, _, _, _, _) <- emp] [7839,7698,7782,7566,7788,7902,7369,7499,7521,7654,7844,7876,7900, 7934] Main> [empno | (empno, _, _, _, _, sal, _) <- emp, sal > 4000] [7839] Main> [empno | (empno, _, _, _, _, sal, _) <- emp, sal > 2000] [7839,7698,7782,7566,7788,7902]

7

Haskell Functions

Hugs> :type product product :: Num a => [a] -> a

product Function

Hugs> product [ 2, 3, 4 ] 24 Hugs> product [ x | x <- [1..10]] 3628800 Hugs> :type (+) (+) :: Num a => a -> a -> a

(+) Operator – an Operator is a 2-ary Function

Hugs> 3 + 5 8 Hugs> (+) 3 5 8 Hugs> (\ x -> x ^ 2 + 4) 5 29

Dr. Philip Cannata

lambdas

8

Haskell Operators

Dr. Philip Cannata

9

Haskell Pattern Matching

Hugs> :type not not :: Bool -> Bool Hugs> not True False Hugs> :type head head :: [a] -> a

not :: Bool  Bool not False = True not True = False

head :: [a] -> a head (x:xs) = x

Hugs> head [5, 4, 3, 2, 1] 5 Hugs> :type map map :: (a -> b) -> [a] -> [b] Hugs> map sqrt [ 1, 4, 9, 10 ] [1.0,2.0,3.0,3.16227766016838]

map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs

Hugs> map (\ x -> x + 2) [ 1, 2, 3, 4] [3,4,5,6]

Dr. Philip Cannata

10

Haskell Pattern Matching

Hugs> product [ 2, 3, 4 ] 24

= = = = =

product :: [Int]  Int product [] = 1 product (x:xs) = x * product xs

product [2,3,4] 2 * product [3,4] 2 * (3 * product [4]) 2 * (3 * (4 * product [])) 2 * (3 * (4 * 1)) 24

Dr. Philip Cannata

11

Haskell Conditional Expressions

Hugs> if True then 6 else 8 6 Hugs> if False then 6 else 8 8

not :: Bool  Bool not False = True not True = False

Let Expressions (i.e., local variables)

Hugs> 2 + let x = sqrt 9 in (x + 1) * (x - 1) 10.0

Let assignment assignment . . . in (expression)

Dr. Philip Cannata

12

Lazy Evaluation: v = 1/0 testLazy x = 2 + 10 testLazy1 x = 2 / x *DBQ> v Infinity *DBQ> testLazy 22 12 *DBQ> testLazy v 12 *DBQ> testLazy1 22 9.090909090909091e-2 *DBQ> testLazy1 v 0.0 *DBQ> testLazy1 1/0 1.#INF Dr. Philip Cannata

Main> (\x -> let y = x in (2 / y)) (1/0) 0.0 Main> (\x -> let y = x in (2 / y)) (0) 1.#INF

13