Skip to content

Instantly share code, notes, and snippets.

@msakai
Created August 16, 2023 01:59
Show Gist options
  • Save msakai/e1ad74b69d5a233203bbb41443550772 to your computer and use it in GitHub Desktop.
Save msakai/e1ad74b69d5a233203bbb41443550772 to your computer and use it in GitHub Desktop.
import Control.Monad
import Turtle
main :: IO ()
main = sh $
forM_ [(1::Int)..2] $ \i -> do
j <- select [(1::Int)..3]
liftIO $ print (i, j)
{-
Actual output:
(1,1)
(2,1)
(2,2)
(2,3)
(1,2)
(2,1)
(2,2)
(2,3)
(1,3)
(2,1)
(2,2)
(2,3)
What I expected:
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
-}
{-
What happens:
Second iteration of the loop (i.e. i=2) is executed INSIDE the branches of
the choice in the first iteration: i.e. (i,j) = (1,1), (1,2), (1,3).
Therefore, we got (2,1), (2,2), (2,3) after each of them.
Lesson learned:
Be careful when using non-determinism inside the loop.
Consider avoiding the mix of loop and non-determinism.
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment