Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zipWith question: flip in rotateUntil #71

Closed
TysonMN opened this issue Aug 8, 2022 · 1 comment
Closed

zipWith question: flip in rotateUntil #71

TysonMN opened this issue Aug 8, 2022 · 1 comment

Comments

@TysonMN
Copy link

TysonMN commented Aug 8, 2022

This is a question about your zipWith article.

Is flip essential in

rotateUntil p xs = zipWith
    (flip const)
    xs
    (dropWhile (not . p) (cycle xs))

?

My guess is that this can be simplified to

rotateUntil p xs = zipWith
    const
    (dropWhile (not . p) (cycle xs))
    xs

Am I correct?

@quchen
Copy link
Owner

quchen commented Jun 15, 2023

The answer is subtle, but the order of zip’s arguments matters:

zipWith (flip const) [] (cycle []) :: [(Int,Int)]
[]
zipWith const (cycle []) [] :: [(Int,Int)]
-- *** Exception: Prelude.cycle: empty list

The reason is that the first parameter is checked first, and when it’s empty, zip terminates without ever looking at the second.

@quchen quchen closed this as completed Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants