1module Main where
2
3import Prelude
4import Effect
5import Control.Monad.ST as ST
6import Control.Monad.ST.Ref as STRef
7import Effect.Console (log, logShow)
8
9collatz :: Int -> Int
10collatz n = ST.run (do
11  r <- STRef.new n
12  count <- STRef.new 0
13  ST.while (map (_ /= 1) (STRef.read r)) do
14    _ <- STRef.modify (_ + 1) count
15    m <- STRef.read r
16    void $ STRef.write (if m `mod` 2 == 0 then m / 2 else 3 * m + 1) r
17  STRef.read count)
18
19main = do
20  logShow $ collatz 1000
21  log "Done"
22