Return to Snippet

Revision: 50505
at August 25, 2011 02:20 by rtperson


Updated Code
{-# LANGUAGE Arrows #-}
import Control.Arrow
import Data.List

list = "abcdefghijklmnop"

-- the trivial version
removeAt 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 things
removeAt''' :: 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

Revision: 50504
at August 25, 2011 02:14 by rtperson


Updated Code
{-# LANGUAGE Arrows #-}
import Control.Arrow
import Data.List

list = "abcdefghijklmnop"

-- the trivial version
removeAt 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
    
main = do
    putStrLn list
    putStrLn $ removeAt 3 list
    putStrLn $ removeAt' 5 list
    putStrLn $ removeAt'' 9 list

Revision: 50503
at August 25, 2011 02:11 by rtperson


Updated Code
{-# LANGUAGE Arrows #-}
import Control.Arrow
import Data.List

-- the trivial version
removeAt 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

Revision: 50502
at August 25, 2011 01:59 by rtperson


Updated Code
import Control.Arrow
import Data.List

-- the trivial version
removeAt 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

Revision: 50501
at August 25, 2011 01:54 by rtperson


Initial Code
-- the trivial version
removeAt 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

Initial URL
http://haskell.org/haskellwiki/99_questions/11_to_20

Initial Description
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.

Initial Title
Haskell 99 Problems - Number 20,  Arrowed!

Initial Tags


Initial Language
Haskell