Home > OS >  Hakell: 'rememberMap' is non lazy
Hakell: 'rememberMap' is non lazy

Time:04-30

I am a newbie to Haskell and I wrote the following code

module RememberMap (rememberMap) where

rememberMap :: (a -> b -> (a, c)) -> a -> [b] -> [c]
rememberMap f acc xs = go acc xs []
        where
          go acc [x] carry = carry <> [step]
            where
              (_, step) = f acc x
          go acc (x:xs) carry = go accStep xs (carry <> [step])
            where
              (accStep, step) = f acc x

I wrote this contaminator with the Intent to help me with the most Common difficulty that i have when writing my Haskell code, That is that I recurrently find myself willing to map something (specially in CodeWarrior's Katas) like to map something, but that something required knowledge of the elements before it. But it had the problem of being non-streaming, ergo, it does no allow me to use lazy proprieties of Haskell with it, thus I would like to know if (a) there is already a solution to this problem (preferably Arrows) or (b) how to make it lazy.

CodePudding user response:

There are mapAccumL and traverse with the lazy State monad.

CodePudding user response:

To make the function stream you need to have the cons operator outside the recursive call, so a caller can see the first element without the whole recursion needing to happen. So you expect it to look something like:

rememberMap f acc (x:xs) = element : ... recursion ...

Once you understand this there is not much more to do:

rememberMap _ _ [] = []
rememberMap f acc (x:xs) = y : rememberMap f acc' xs
  where
    (acc', y) = f acc x

You can make an auxiliary function to avoid passing f around if you want, but there's no reason for it to have the extra list that you called carry.

  • Related