Make <|> for ZipLists lazy for infinite lists
Andreas Abel
abela at chalmers.se
Wed Jun 6 06:43:24 UTC 2018
I prefer the original definition
ZipList xs <|> ZipList ys = ZipList (xs ++ drop (length xs) ys)
for sake of clarity.
On 05.06.2018 09:00, Chris Wong wrote:
> The proposed definition has one drawback: it is strict in its second
> argument.
>
> It should be possible to make it lazy in its second argument while
> keeping the single-pass behavior, though. Something like this?
>
> ZipList xs <|> ZipList ys = ZipList $ go xs ys 0
> where
> go [] ys n = drop n ys
> go (x:xs) ys n = x : (go xs ys $! n + 1)
>
> On Tue, Jun 5, 2018, 15:36 박신환 <ndospark320 at naver.com
> <mailto:ndospark320 at naver.com>> wrote:
>
> Current definion of (<|>) for ZipLists:
>
> ZipList
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList>xs
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319881><|>
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.Base.html#%3C%7C%3E>ZipList
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList>ys
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319882>=ZipList
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList>(xs
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319881>++
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.Base.html#%2B%2B>drop
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.List.html#drop>(length
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Data.Foldable.html#length>xs
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319881>)ys
> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319882>)
>
> doesn't work if the left argument is infinite. It should be:
>
> ZipList [] <|> ys = ys
> xs <|> ZipList [] = xs
> ZipList (x:xs) <|> ZipList (_:ys) = ZipList (x : (ZipList xs <|>
> ZipList ys))
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org <mailto:Libraries at haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
>
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
--
Andreas Abel <>< Du bist der geliebte Mensch.
Department of Computer Science and Engineering
Chalmers and Gothenburg University, Sweden
andreas.abel at gu.se
http://www.cse.chalmers.se/~abela/
More information about the Libraries
mailing list