Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created August 5, 2010 18:29
Show Gist options
  • Select an option

  • Save Koitaro/510156 to your computer and use it in GitHub Desktop.

Select an option

Save Koitaro/510156 to your computer and use it in GitHub Desktop.
definition module BinaryTree
:: BinaryTree a = Node a (BinaryTree a) (BinaryTree a) | Leaf
(!!!) infixl 9 :: (BinaryTree a) Int -> a
implementation module BinaryTree
import StdEnv
(!!!) infixl 9 :: (BinaryTree a) Int -> a
(!!!) tree n = f root tree where
f [] (Node a _ _) = a
f [x:xs] (Node _ tl tr)
| x == 0 = f xs tl
| x == 1 = f xs tr
root = (tl o f [] o (+) 1) n where
f xs 0 = xs
f xs n = f [n rem 2:xs] (n/2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment