[Haskell-beginners] lifting to applicative: recomputing an argument each time it is used?

Iain Nicol iain at thenicols.net
Sun Sep 9 01:29:38 CEST 2012


On 2012-09-09, Chaddaï Fouché <chaddai.fouche at gmail.com> wrote:
> On Sat, Sep 8, 2012 at 4:49 PM, Iain Nicol <iain at thenicols.net> wrote:
>
>> Hi,
>>
>> I think I'm trying to lift 'Data.List.intersperse' (to applicative or
>> a monad) in such a way that its (first) argument is recomputed each
>> time it is used.
>
> Instead of using intersperse, just generate two list and interlace
> them (interlace is easy to write, though not in Data.List [...] That
> seems more elegant to me but you'll judge :)

I appreciate the response.  Your suggestion was indeed significantly
cleaner than what I had come up with.  And your second response has
encouraged me to explore the "Test.QuickCheck.Modifiers" module in
general.

Nonetheless, I was still hoping to reuse the intersperse function, and
so I spent "a little" bit more time on this problem.  After hours of
experimenting in the wrong direction, the following accidentally came to
me:


import Test.QuickCheck (elements, Gen, sized)
import Data.List (intersperse)

mixIntersperse :: Gen String -> Gen String -> Gen [String]
mixIntersperse genSep genWord =
  sized (sequence . intersperse genSep . (`replicate` genWord))


Thanks,
--
Iain



More information about the Beginners mailing list