Skip to content

Commit

Permalink
edits
Browse files Browse the repository at this point in the history
  • Loading branch information
vidsinghal committed Jan 26, 2024
1 parent 9918357 commit 3da170f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module TreeAddOne where


data PackedInt = P Int
data Tree = Node Tree Tree PackedInt | Nil


mkTree :: Int -> Tree
mkTree depth = if depth <= 0
then Nil
else
let
left = mkTree (depth-1)
right = mkTree (depth-1)
val = P depth
in Node left right val

addOne :: PackedInt -> PackedInt
addOne val = case val of
P b -> P (b+1)

addOneTree :: Tree -> Tree
addOneTree tree = case tree of
Node l r val -> let
a1 = addOne val
sl = addOneTree l
sr = addOneTree r
in Node sl sr a1
Nil -> Nil



gibbon_main = let
tree = mkTree 27
newTree = iterate (addOneTree tree)
in ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module TreeSum where

data PackedInt = P Int
data Tree = Node PackedInt Tree Tree | Leaf


mkTree :: Int -> Tree
mkTree depth = if depth <= 0
then Leaf
else
let
val = P depth
left = mkTree (depth-1)
right = mkTree (depth-1)
in Node val left right


copyPackedInt :: PackedInt -> PackedInt
copyPackedInt a = case a of
P b -> P b

copyTree :: Tree -> Tree
copyTree tree = case tree of
Node val l r -> let
newVal = copyPackedInt val
sl = copyTree l
sr = copyTree r
in Node newVal sl sr
Leaf -> Leaf



gibbon_main = let
tree = mkTree 27
newTree = iterate (copyTree tree)
in () --printPacked newTree
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module TreeExpo where

data PackedInt = P Int
data Tree = Node PackedInt Tree Tree | Nil


mkTree :: Int -> Tree
mkTree depth = if depth <= 0
then Nil
else
let
val = P depth
left = mkTree (depth-1)
right = mkTree (depth-1)
in Node val left right

expo :: Int -> Int -> Int
{-# INLINE expo #-}
expo power val = if power == 0
then val
else val * expo (power-1) val


expo2 :: PackedInt -> PackedInt
expo2 val = case val of
P x -> P (expo 10 x)

expoTree :: Tree -> Tree
expoTree tree = case tree of
Node val l r -> let
a1 = expo2 val
sl = expoTree l
sr = expoTree r
in Node a1 sl sr
Nil -> Nil



gibbon_main = let
tree = mkTree 27
sum = iterate (expoTree tree)
in ()

0 comments on commit 3da170f

Please sign in to comment.