Skip to content

Instantly share code, notes, and snippets.

@limansky
limansky / grouplist.hs
Created December 22, 2011 15:40
Problem of grouping list values.
-- ghci example
-- *Main> group [1,2,3,4,7,8,13]
-- [(1,4),(7,8),(13,13)]
group = foldr f []
where f x [] = [(x,x)]
f x as@((a,b):as') = if a == x+1 then (x,b):as'
else (x,x):as
-- *Main> ungroup [(1,4),(7,8),(13,13)]
-- [1,2,3,4,7,8,13]
ungroup [] = []
@limansky
limansky / numbers.hs
Created December 19, 2011 23:41
The problem of list order
import Data.List (delete)
squarable x = x `elem` [y^2 | y <- [2..x] ]
nexts x rest = foldr check [] rest
where check v good = if squarable (x + v) then v:good
else good
search ls [] = [ls]
search ls rs = nexts (last ls) rs >>= \v -> search (ls ++ [v]) (delete v rs)