Skip to content

Instantly share code, notes, and snippets.

@mmitou
Created November 7, 2013 03:26
Show Gist options
  • Select an option

  • Save mmitou/7348481 to your computer and use it in GitHub Desktop.

Select an option

Save mmitou/7348481 to your computer and use it in GitHub Desktop.
継続渡し的なsplit
replace :: Int -> Int -> [a] -> [a]
replace i j xs | 0 <= i && i < j && j < length xs = mkHead . (:) xj . mkBody . (:) xi $ tail
| null xs = []
| otherwise = xs
where
(mkHead, (xi:bodyTail)) = splitCps i xs id
(mkBody, (xj:tail)) = splitCps (j - i - 1) bodyTail id
splitCps :: Int -> [a] -> ([a] -> [a]) -> ([a] -> [a], [a])
splitCps 0 xs f = (f, xs)
splitCps _ [] f = (f, [])
splitCps n (x:xs) f = splitCps (n - 1) xs (\ys -> f (x:ys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment