Skip to content

Instantly share code, notes, and snippets.

@KirinDave
Created March 8, 2018 18:23
Show Gist options
  • Save KirinDave/36729c4f67f9aaecaf7dac898ea3899e to your computer and use it in GitHub Desktop.
Save KirinDave/36729c4f67f9aaecaf7dac898ea3899e to your computer and use it in GitHub Desktop.
-- Normal data strcutures
data RecursiveList a = RNil
| RCons a (RecursiveList a)
exampleRlist :: RecursiveList Int
exampleRlist = RCons 1 (RCons 2 RNil)
foldRecList :: (b -> a -> b) -> RecursiveList a -> b -> b
foldRecList f lst basecase = helper basecase lst
where
helper cur (RCons v rest) = helper (f cur v) rest
helper cur RNil = cur
summedList = foldRecList (+) exampleRlist 0
data RecursiveSparseTrie a = RLeaf a
| RNode (RecursiveSparseTrie a)
(RecursiveSparseTrie a)
exampleRTree :: RecursiveSparseTrie Int
exampleRTree = RNode (RLeaf 1) (RLeaf 2)
foldRecTree :: (b -> a -> b) -> RecursiveSparseTrie a -> b -> b
foldRecTree f tree basecase = helper basecase tree
where
helper cur (RLeaf v) = f cur v
helper cur (RNode s1 s2) = helper (helper cur s1) s2
summedTree = foldRecTree (+) exampleRTree 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment