juan_gandhi: (Default)
Juan-Carlos Gandhi ([personal profile] juan_gandhi) wrote2010-01-29 08:30 pm

how to say in Haskell...


pairwise :: [a] →  [(a, a)]
pairwise [] = []
pairwise (x:y:rest) = (x,y) : pairwise rest

is there something that does it already? Could not figure out.

полет фантазии

[identity profile] huzhepidarasa.livejournal.com 2010-01-30 04:15 pm (UTC)(link)
pairwise xs = uncurry zip $ pairmap (map snd) $ partition fst $ zip (cycle [True, False]) xs
  where pairmap f (x,y) = (f x, f y)

хотел без рекурсии и не определяя никаких вспомогательных ф-й, но не вышло

Re: полет фантазии

[identity profile] huzhepidarasa.livejournal.com 2010-01-30 04:21 pm (UTC)(link)
объединяя с предыдущей идеей

pairwise xs = map snd $ filter fst $ zip (cycle [True, False]) $ zip xs (tail xs)

Re: полет фантазии

[identity profile] ivan-gandhi.livejournal.com 2010-01-30 06:06 pm (UTC)(link)
О! cycle приходит на помощь!

Re: полет фантазии

[identity profile] nivanych.livejournal.com 2010-01-30 06:17 pm (UTC)(link)
Прикольная идея ;-)