Skip to content

Instantly share code, notes, and snippets.

@gmp26
Created October 26, 2014 08:00
Show Gist options
  • Save gmp26/d9ad2fca58be1c35b11e to your computer and use it in GitHub Desktop.
Save gmp26/d9ad2fca58be1c35b11e to your computer and use it in GitHub Desktop.
Left and right numbered entries in a stack.
module StackPair where
import Either as E
import Either (Either (..), lefts, rights)
type Numbered a = {a | n: Int}
type Entry a = Either (Numbered a) (Numbered a)
type Entries a = [Entry a]
{-
add a new entry to left or right stack
-}
add: Entry a -> Entries a -> Entries a
add = (::)
{-
Heights of a numberd stack
stackHeights [1,4,7] = [0,1,5,12]
-}
stackHeights : [Numbered a] -> [Int]
stackHeights s = scanl (\na x -> x + na.n) 0 (reverse s)
{-
heightsOf lefts [Left 1, Right 3, Left 9] == [0,1,10]
-}
heightsOf : (Entries a -> [Numbered a]) -> Entries a -> [Int]
heightsOf onSide = onSide >> stackHeights
-- This incorrect version causes the 0.13 compiler to spin indefinitely
-- heightsOf : (Entries a -> [a]) -> Entries a -> [Int]
-- heightsOf onSide = onSide >> stackHeights
main : Element
main = []
|> add (Left {n=1})
|> add (Right {n=3})
|> add (Right {n=5})
|> add (Left {n=4})
|> add (Left {n=7})
|> heightsOf lefts
|> asText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment