Commit 63e1e7f3 authored by Martin Wierich's avatar Martin Wierich
Browse files

new Bag type: :: Bag x = Pair (Bag x) (Bag x) | Single x | Empty

using this for accumulators is more elegant than using lists
parent 6402c61b
......@@ -115,3 +115,8 @@ iterateSt op st :== iterate_st op st
revAppend :: ![a] ![a] -> [a] // Reverse the list using the second argument as accumulator.
revMap :: !(.a -> .b) ![.a] !u:[.b] -> u:[.b]
:: Bag x = Empty | Single !x | Pair !(Bag x) !(Bag x)
uniqueBagToList :: !*(Bag x) -> [x] // exploits reuse of unique nodes (if compiled with that option)
bagToList :: !(Bag x) -> [x]
isEmptyBag :: !(Bag x) -> Bool
......@@ -222,3 +222,33 @@ zip2Append [] [] tail
zip2Append [x : xs] [y : ys] tail
= [(x,y) : zip2Append xs ys tail]
*/
:: Bag x = Empty | Single !x | Pair !(Bag x) !(Bag x)
uniqueBagToList :: !*(Bag x) -> [x] // exploits reuse of unique nodes (if compiled with that option)
uniqueBagToList bag
= accumulate_elements bag []
where
accumulate_elements :: !*(Bag x) [x] -> [x]
accumulate_elements Empty accu
= accu
accumulate_elements (Single element) accu
= [element : accu]
accumulate_elements (Pair bag1 bag2) accu
= accumulate_elements bag1 (accumulate_elements bag2 accu)
bagToList :: !(Bag x) -> [x]
bagToList bag
= accumulate_elements bag []
where
accumulate_elements :: !(Bag x) [x] -> [x]
accumulate_elements Empty accu
= accu
accumulate_elements (Single element) accu
= [element : accu]
accumulate_elements (Pair bag1 bag2) accu
= accumulate_elements bag1 (accumulate_elements bag2 accu)
isEmptyBag :: !(Bag x) -> Bool
isEmptyBag Empty = True
isEmptyBag _ = False
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment