import "prelude.eg" namespace Hailstone ( using System using List def even = [ N -> (N%2) == 0 ] def hailstone = [ 1 -> {1} | N -> if even N then cons N (hailstone (N/2)) else cons N (hailstone (N * 3 + 1)) ] def hailpair = [ N -> (N, length (hailstone N)) ] def hailmax = [ (N, NMAX), (M, MMAX) -> if (NMAX < MMAX) then (M, MMAX) else (N, NMAX) ] def largest = [ 1 -> (1, 1) | N -> let M0 = hailpair N in let M1 = largest (N - 1) in hailmax M0 M1 ] ) using System using List using Hailstone def task0 = let H27 = hailstone 27 in length H27 def task1 = let H27 = hailstone 27 in let L = length H27 in (take 4 H27, drop (L - 4) H27) def task2 = largest 100000 def main = (task0, task1, task2)