[Haskell-cafe] "Wrong kind" when attempting to build a monad for
a circular list of functions
Roel van Dijk
vandijk.roel at gmail.com
Sun Mar 2 08:21:29 EST 2008
Looks good! A few tips:
> funcList :: [Int -> Int]
> funcList = [\_ -> 1, \_ -> 2, \_ -> 3]
funcList = [const 1, const 2, const 3]
>
> iterateCircularFL :: [a -> b] -> (a -> b, [a -> b])
> iterateCircularFL (x:xs) = (x, concat [xs, [x]])
{- If you use cycle in main then you do not need this function at all. -}
> applyCircularFL :: a -> [a -> b] -> (b, [a -> b])
> applyCircularFL arg fList =
> let
> (currentFunc, iteratedList) = iterateCircularFL fList
> in (currentFunc arg, iteratedList)
{- If the list of functions is infinite then we do not have to worry
about exhausting it, although an empty list will still cause a pattern
match failure. -}
applyCircularFL :: a -> [a -> b] -> (b, [a -> b])
applyCircularFL arg (f:fs) = (f arg, fs)
>testTraversal i l
> | i == 0 = putStr "Done."
> | i > 0 = do {
> putStr "Execution ";
> putStr (show i);
> putStr " returned ";
> putStr (show val);
> putStr ".\n";
> testTraversal (i - 1) newList
> }
> where (val, newList) = applyCircularFL i l
{- Transform funcList into an infinite list to simplify things -}
main = testTraversal 5 $ cycle funcList
I hope these tips are usefull :-)
Roel
More information about the Haskell-Cafe
mailing list