Sei sulla pagina 1di 16

A Tutorial on HASKELL

Fahmida Hamid
HASKELL: Teach Yourself
Useful Commands
How to Compile / Exit / Load / Re-load
$ ghci
Prel ude >: qui t
Prel ude >: l f i l eName
Prel ude >: r
Fahmida Hamid | University of North Texas Department of CSE 2/16
HASKELL: Teach Yourself
Test Several Builtin Commands and List Structure
Prel ude >1 + 1
Prel ude >[ 1 . . 1 0 ]
Prel ude>length [ 1 . . 10]
Prel ude >[ 2 , 4 . . 1 5 ]
Prel ude>length [ 2 , 4 . . 1 5 ]
Fahmida Hamid | University of North Texas Department of CSE 3/16
HASKELL: Teach Yourself
Lists, Characters, Strings
head [ a , b , c ]
t ai l [ " appl e " , " orange " , " st r awber r y " ]
length [ a . . z ]
" Hel l o " ++ " " ++ " World ! "
gcd 67 22
sum [ 1 , 2 , 3 , 4 ]
Fahmida Hamid | University of North Texas Department of CSE 4/16
HASKELL: Teach Yourself
User Dened Functions: Arithmetic Operations
add x y = x + y
{ implement s ubt r act i on , mul t i pl i c at i on , di v i s i o n }
A simple Recursive Function
sum1 [ ] = 0
sum1 ( x : xs ) = x + sum1 xs
Fahmida Hamid | University of North Texas Department of CSE 5/16
HASKELL: Teach Yourself
Example: Fibonacci Series
f i bo 0 = 0
f i bo 1 = 1
f i bo n = f i bo ( n1) + f i bo ( n 2)
Print the Series
map f i bo [ 0 . . 5 ]
Not Fast at all!
Fahmida Hamid | University of North Texas Department of CSE 6/16
HASKELL: Teach Yourself
Fibonacci Series : n > 20
f i bo n r p | n == 1 = r
f i bo n r p = f i bo ( n1) ( r+p) r
f i bTa i l n | n == 0 = 0
f i bTa i l n = f i bo n 1 0
Print the Series
map f i bTa i l [ 0 . . 5 0 ]
Test it at home and gure out!
Fahmida Hamid | University of North Texas Department of CSE 7/16
HASKELL: Teach Yourself
C Program : Add Two Numbers
// i nc l ude s t dl i b . h and s t di o . h
t ypedef l ong l ong Nat ;
Nat succ ( Nat n) { r et ur n n + 1; }
Nat pred ( Nat n) { r et ur n n 1; }
Nat add( Nat x , Nat y) { i f ( x == 0) r et ur n y ;
e l s e r et ur n succ ( add( pred ( x) , y) ) ;
}
i nt main ( ) { Nat x=10L; Nat y=20L;
pr i nt f ( " add(%l l d ,% l l d )=%l l d \n" , x , y , add( x , y) ) ;
}
Fahmida Hamid | University of North Texas Department of CSE 8/16
HASKELL: Teach Yourself
Addition
module Peano where
data Nat = Zero | S Nat de r i vi ng ( Read , Show)
add Zero n = n
add ( S x) y = S ( add x y)
Fahmida Hamid | University of North Texas Department of CSE 9/16
HASKELL: Teach Yourself
Sample Run Scenario
Peano> : r
Ok, modules l oaded : Peano .
Peano> add Zero Zero
Zero
Peano> add ( S Zero ) Zero
S Zero
Peano> add ( S Zero ) ( S ( S Zero ) )
S ( S ( S Zero ) )
Peano>
Fahmida Hamid | University of North Texas Department of CSE 10/16
HASKELL: Teach Yourself
Subtraction
Lets denote any Natural Number with symbol _
Subtracting Zero from _ gives _
Subtracting y from x is equal to Subtracting y 1 from x 1
Probable Output
Peano> sub ( S( S Zero ) ) ( S Zero )
S Zero
Peano> sub ( S( S Zero ) ) ( Zero )
S ( S Zero )
Peano>
Fahmida Hamid | University of North Texas Department of CSE 11/16
HASKELL: Teach Yourself
Multiplication
mul : : Nat > Nat > Nat
mul Zero _ = Zero
mul ( S x) y = add y ( mul x y)
Sample Run Scenario
Peano> mul ( S ( S Zero ) ) ( S ( S Zero ) )
S ( S ( S ( S Zero ) ) )
Peano> mul Zero ( S Zero )
Zero
Fahmida Hamid | University of North Texas Department of CSE 12/16
HASKELL: Teach Yourself
Logical Equivalence(==)
eq Zero Zero = True
eq Zero ( S _) = Fal s e
eq ( S _) Zero = Fal s e
eq ( S x) ( S y) = eq x y
Sample Run Scenario
Peano> eq ( S Zero ) Zero
Fal s e
Peano> eq ( S Zero ) ( S Zero )
True
Fahmida Hamid | University of North Texas Department of CSE 13/16
HASKELL: Teach Yourself
Greater Than(>)
Lets denote any Natural Number with symbol _
Zero cannot be greater than itself!(since this is not >=)
Zero cannot be greater than _
_ is always greater than Zero
x > y means (x 1) > (y 1)
Fahmida Hamid | University of North Texas Department of CSE 14/16
HASKELL: Teach Yourself
Probable Output
Peano> gt Zero Zero
Fal s e
Peano> gt Zero ( S Zero )
Fal s e
Peano> gt ( S ( S Zero ) ) ( S Zero )
True
Fahmida Hamid | University of North Texas Department of CSE 15/16
Questions?
Ready for the Homework!!!

Potrebbero piacerti anche