## Posted By

rtperson on 08/25/11

# Haskell 99 Problems - Number 20, Arrowed!

/ Published in: Haskell

problem 20, (*) Remove the K'th element from a list

``````*Main> removeAt 1 "abcd"
"acd"
``````

Trivial using a pure function. A bit more challenging if you use this problem to work up your Arrow-fu.

`{-# LANGUAGE Arrows #-}import Control.Arrowimport Data.List list = "abcdefghijklmnop" -- the trivial versionremoveAt n xs = take (n-1) xs ++ (drop n) xs -- now new and improved! with Arrows for ultra-obfuscation!removeAt' :: (Arrow cat) => Int -> cat [a] [a]removeAt' n = arr(\ xs -> (xs,xs)) >>> arr (take (n-1)) *** arr (drop n)                     >>> arr (uncurry (++)) >>> returnA {-   the arrowed function is easier to understand in its sugary form   NOTE: the head and tail of the arrows are lambda values, so they   can't be used inside the arrow's machinery. Also, to use this    notation, you'll need the LANGUAGE Arrows pragma-}removeAt'' :: Int -> [a] -> [a]removeAt'' n = proc things -> do    (begin, end) <- (\xs -> (xs, xs)) -< things    begin' <- take (n-1) -< begin    end' <- drop n -< end    newlist <- uncurry (++) -< (begin', end')    returnA -< newlist -- This is equivalent, and shows how proc notation can-- simplify thingsremoveAt''' :: Int -> [a] -> [a]removeAt''' n = proc things -> do    begin <- take (n-1) -< things    end <- drop n -< things    returnA -< begin ++ end main = do    putStrLn list    putStrLn \$ removeAt 3 list    putStrLn \$ removeAt' 5 list    putStrLn \$ removeAt'' 9 list`