Skip to content

Instantly share code, notes, and snippets.

@faust45
Created March 13, 2011 17:48
Show Gist options
  • Select an option

  • Save faust45/868287 to your computer and use it in GitHub Desktop.

Select an option

Save faust45/868287 to your computer and use it in GitHub Desktop.
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
singleton :: a -> Tree a
singleton x = Node x EmptyTree EmptyTree
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x (Node a left right)
| x == a = Node x left right
| x < a = Node a (treeInsert x left) right
| x > a = Node a left (treeInsert x right)
treeElem :: (Ord a) => a -> Tree a -> Bool
treeElem x EmptyTree = False
treeElem x (Node a left right)
| x == a = True
| x < a = treeElem x left
| x > a = treeElem x right
nums = [8,6,4,1,7,3,5]
n = treeInsert 5 EmptyTree
a = treeInsert 30 n
c = treeInsert 93 a
numsTree = foldr treeInsert EmptyTree nums
res = map (+1) [1,2,3,4,5]
f :: Maybe a -> Maybe b
f Nothing = Nothing
f (Just x) = Just (x * 2)
main = print (res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment