Skip to content

Instantly share code, notes, and snippets.

@miladhub
Last active October 16, 2018 13:20
Show Gist options
  • Save miladhub/6b469a42c37ebb176696f63acee23c79 to your computer and use it in GitHub Desktop.
Save miladhub/6b469a42c37ebb176696f63acee23c79 to your computer and use it in GitHub Desktop.
Inverting a binary tree (Haskell)
module BinTrees where
data BinTree a =
Node a (BinTree a) (BinTree a)
| Nil
deriving (Show, Eq)
leaf :: a -> BinTree a
leaf a = Node a Nil Nil
inverse :: BinTree a -> BinTree a
inverse Nil = Nil
inverse (Node a l r) = Node a (inverse r) (inverse l)
{-
$ stack ghci
Warning: No local targets specified, so ghci will not use any options from your package.yaml / *.cabal files.
Potential ways to resolve this:
* If you want to use the package.yaml / *.cabal package in the current directory, use stack init to create a
new stack.yaml.
* Add to the 'packages' field of /Users/milad.bourhani/.stack/global-project/stack.yaml
Configuring GHCi with the following packages:
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /private/var/folders/py/ps94yrz97g996d0qtnbsjtbcvtv54l/T/ghci41025/ghci-script
Prelude> :l bintree.hs
[1 of 1] Compiling BinTrees ( bintree.hs, interpreted )
Ok, modules loaded: BinTrees.
*BinTrees> tree = Node 1 ( Node 2 (leaf 4) (leaf 5) ) ( Node 3 (leaf 6) (leaf 7) )
*BinTrees> inverse tree
Node 1 (Node 3 (Node 7 Nil Nil) (Node 6 Nil Nil)) (Node 2 (Node 5 Nil Nil) (Node 4 Nil Nil))
*BinTrees>
Leaving GHCi.
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment