[Haskell-cafe] "Wrong kind" when attempting to build a monad for
a circular list of functions
Felipe Lessa
felipe.lessa at gmail.com
Thu Feb 28 05:44:48 EST 2008
On Thu, Feb 28, 2008 at 4:28 AM, Aaron Altman <aaltman at pdx.edu> wrote:
> runActionAndIterate :: [a -> a] -> a -> (a, [a -> a])
> runActionAndIterate (currentAction:actionList) actionInput =
> (currentAction actionInput, concat [actionList, [currentAction]])
>
> shiftActionList :: [a -> a] -> [a -> a]
> shiftActionList (currentHead:rest) = concat [rest, [currentHead]]
As a side note, it's not good to recreate the list (using 'concat')
for every item as it is an O(n) operation. Bas van Dijk's 'always'
(also called 'forever'[1]) is an option, but you can also create a
circular list using the simple function 'cycle'[2] and your functions
above would become
runActionAndIterate (currentAction:actionList) actionInput =
(currentAction actionInput, actionList)
shiftActionList = tail
[1] http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#v%3Aforever
[2] http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Acycle
--
Felipe.
More information about the Haskell-Cafe
mailing list