Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active December 15, 2015 18:49
Show Gist options
  • Select an option

  • Save WillNess/5306459 to your computer and use it in GitHub Desktop.

Select an option

Save WillNess/5306459 to your computer and use it in GitHub Desktop.
triags ... ... mapped patterns
{-# LANGUAGE PatternGuards #-}
-- triangular enumeration by upward diagonals
triags :: [[a] -> [a]
triags s = g s []
where
g [] [] = []
g s a = let (h,t) = splitAt 1 s
acc = filter (not.null) $ h ++ a
in map head acc ++ g t (map tail acc)
pairs :: [a] -> [b] -> [(a, b)]
pairs xs ys = triags [ [(x,y) | y<-ys] | x<-xs]
{- -- http://stackoverflow.com/questions/15792912/haskell-two-lists-into-a-list-of-tuples
*Main> triags [0..4] ['A'..'C']
[(0,'A'),(1,'A'),(0,'B'),(2,'A'),(1,'B'),(0,'C'),(3,'A'),(2,'B'),(1,'C'),(4,'A')
,(3,'B'),(2,'C'),(4,'B'),(3,'C'),(4,'C')]
-}
-- more general:
-- triangular enumeration by upward diagonals
triags :: [[a]] -> [a]
triags s = g s []
where
g s a = let (h,t) = splitAt 1 s in
case (filter (not.null) $ h ++ a, t)
of { ([],[]) -> [] ;
(acc,_) -> map head acc ++ g t (map tail acc) }
pairs xs ys = triags [ [(x,y) | y<-ys] | x<-xs]
--------
unique xs = [x | [x] <- group . sort $ xs]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment