module Auxfuns where

import Data.List(sort,nub)

canonical x = nub(sort x)
union x y = canonical(x ++ y)
unions xs = foldr union [] xs


ps :: [a] -> [[a]]
ps [] = [[]]
ps (a:l) = let pl = ps l
              in pl ++ map (a:) pl

powerSet xs = map canonical (ps xs)


plistf :: (a -> String) -> String -> [a] -> String -> String -> String
plistf f open xs sep close = open ++ help xs ++ close
  where help [] = ""
        help [x] = f x
        help (x:xs) = f x ++ sep ++ help xs    

comp x y | length x < length y = LT
comp x y | length y < length x = GT
comp x y = compare x y

--  insertByCons:  Extend an association list of lists by cons
insertByCons :: (Eq k) => [(k, [v])] -> k -> v -> [(k, [v])]
insertByCons [] k v = [(k,[v])]
insertByCons ((p,fp):f) k v 
    | p == k = (p,v:fp):f
    | otherwise = (p,fp):(insertByCons f k v)

