Download presentation
Presentation is loading. Please wait.
1
Programming Languages
Haskell Part 1
2
A programming language is a language with a well-defined syntax (lexicon and grammar), type system, and semantics that can be used to implement a set of algorithms. Haskell
3
Haskell: /lusr/bin/hugs, should be in your default $PATH or for windows, download and install winhugs at $ hugs __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) ||---|| ___|| World Wide Web: || || Bugs: || || Version: _________________________________________ Haskell 98 mode: Restart with command line option -98 to enable extensions Type :? for help Hugs> :load 09H1 Main> To load the file “09H1.hs” from the directory in which you started hugs
4
Haskell Expression Number Expressions Boolean Expressions
Hugs> 2 * 4 ^2 32 Hugs> 8^2 64 Hugs> (2 * 4) ^ 2 Hugs> ^ 2 18 Hugs> 2 ^ 200 Hugs> True && False False Hugs> True || False True Hugs> 3 < 5 Hugs> 'c' < 'p' Hugs> 'c' > 'p' Hugs> "tree" < "rock" Hugs> "tree" > "rock" Number Expressions Boolean Expressions Character Expressions String Expressions
5
Haskell Basic Data Structures
Hugs> ("dog", "cat", 5) ("dog","cat",5) Hugs> ["dog", "cat", 5] ERROR - Cannot infer instance *** Instance : Num [Char] *** Expression : ["dog","cat",5] Hugs> ["dog", "cat", "5"] ["dog","cat","5"] Hugs> 1 : [] [1] Hugs> 1 : [2,3,4] [1,2,3,4] [expression | generator] Hugs> [2 * x ^ 2 | x <- [1, 2, 3 ,4]] [2,8,18,32] Hugs> [x * y | (x, y) <- [(1, 2), (3, 4), (5, 6)]] [2,12,30] Tuples Lists List Construction List Comprehension Haskell Basic Data Structures
6
Haskell Basic Data Structures continued
List Comprehension Cross product “<“ and “>” Relation Factors of 72 Hugs> [(x, y) | x <- [1, ], 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)] 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]
7
Haskell Basic Data Structures - continued
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]
8
Haskell Functions Hugs> :type product
product :: Num a => [a] -> a Hugs> product [ 2, 3, 4 ] 24 Hugs> product [ x | x <- [1..10]] Hugs> :type (+) (+) :: Num a => a -> a -> a Hugs> 3 + 5 8 Hugs> (+) 3 5 Hugs> (\ x -> x ^ 2 + 4) 5 29 product Function (+) Operator – an Operator is a 2-ary Function lambdas
9
Haskell Operators
10
Haskell Pattern Matching
Hugs> :type not not :: Bool -> Bool Hugs> not True False Hugs> :type head head :: [a] -> a 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, ] Hugs> map (\ x -> x + 2) [ 1, 2, 3, 4] [3,4,5,6] not :: Bool Bool not False = True not True = False head :: [a] -> a head (x:xs) = x map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs
11
= = = = = Haskell Pattern Matching product [2,3,4] 2 * product [3,4]
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 =
12
Haskell Conditional Expressions
Let Expressions (i.e., local variables) Hugs> if True then 6 else 8 6 Hugs> if False then 6 else 8 8 Hugs> 2 + let x = sqrt 9 in (x + 1) * (x - 1) 10.0 not :: Bool Bool not False = True not True = False Let assignment assignment . in (expression)
13
Lazy Evaluation: v = 1/0 testLazy x = testLazy1 x = 2 / x *DBQ> v Infinity *DBQ> testLazy 22 12 *DBQ> testLazy v *DBQ> testLazy1 22 e-2 *DBQ> testLazy1 v 0.0 *DBQ> testLazy1 1/0 1.#INF Main> (\x -> let y = x in (2 / y)) (1/0) 0.0 Main> (\x -> let y = x in (2 / y)) (0) 1.#INF
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.