






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A part of the fall '05 cs457/557 functional programming course notes at psu, focusing on the topic of trees. Trees are essential data structures in computer science, characterized by their finite, unbounded size, polymorphism, and various branching factors. Different types of trees, such as integertree, simpletree, itree, and fancytree, and discusses functions on trees, including maptree, fringe, treesize, treeheight, and elemtree. It also introduces binary search trees and their implementation.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!
insertTree::Ord a => a -> ITree a -> ITree a insertTree v ILeaf = IBranch v ILeaf ILeaf insertTree v (IBranch w l r) | v <= w = IBranch w (insertTree v l) r | v > w = IBranch w l (insertTree v r) listToTree xs = foldr insertTree ILeaf xs s = listToTree [1,4,3,5,2,9,8] == (IBranch 8 (IBranch 2 (IBranch 1 ILeaf ILeaf) (IBranch 5 (IBranch 3 ILeaf (IBranch 4 ILeaf ILeaf)) ILeaf)) (IBranch 9 ILeaf ILeaf))
data Expr = C Float | Add Expr2 Expr | Sub Expr2 Expr | Mul Expr2 Expr | Div Expr2 Expr Or, using infix constructor names: data Expr = C Float | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr Infix constructors begin with a colon (:) , whereas ordinary constructor functions begin with an upper-case character.
e1 = (C 10 :+ (C 8 :/ C 2)) :* (C 7 :- C 4) evaluate :: Expr -> Float evaluate (C x) = x evaluate (e1 :+ e2) = evaluate e1 + evaluate e evaluate (e1 :- e2) = evaluate e1 - evaluate e evaluate (e1 :* e2) = evaluate e1 * evaluate e evaluate (e1 :/ e2) = evaluate e1 / evaluate e Main> evaluate e