Docsity

Advanced Haskell Features - Lecture Slides | CSSE 490, Study notes of Computer Science

Partial preview of the text

Download Advanced Haskell Features - Lecture Slides | CSSE 490 and more Study notes Computer Science in PDF only on Docsity!

ADVANCED HASKELL

FEATURES

Curt Clifton Rose-Hulman Institute of Technology

Please SVN Update your HaskellInClass folder,

then open Trees.hs

ADVANCED HASKELL

FEATURES

Modules Pattern matching on data types Guards Testing Instance declarations Infix operators

PATTERN MATCHING AND

GUARDS

data Pair a b = Pair a b twins :: Eq a => Pair a a -> String twins (Pair x y) | x == y = “yep” | otherwise = “nope” Matches a pair and binds x and y Guards work like case but use values instead of patterns Q

TESTING

See sample code for testing depth

INFIX OPERATORS

Surrounding binary function names with backticks lets us use them as infix operators: Hugs> 4 div 2 2 Surround infix operators with parentheses lets us declare their types: (<~) :: BinaryTree a -> BinaryTree a -> BinaryTree a _ <~ ExtNode = error "Can’t attach tree to ext. node" Q