module Functions where

import POSIX

z = \x -> x + 3
y = z 5
a = (\b -> b - 7) 15

fcn = \a b c -> a + b - c
fcn1 = fcn 13
-- fcn1 = (\a b c -> a + b - c) 13 = \b c -> 13 + b - c
f = fcn1 10 20

myfun = \f g x -> f (g (g x))
m = myfun (\x -> x) (\a -> a + 3) 10
myfun1 = myfun (\y -> y + 5)

myfun2 = \a b -> myfun a (\x -> x) b
n = myfun2 (\x -> x+2) 5
{-  
    myfun2 (\x -> x+2) 5 = 
    (\a b -> myfun a (\x -> x) b) (\x -> x+2) 5 = 
    (\b -> myfun (\x -> x+2) (\x -> x) b) 5 = 
    myfun (\x -> x+2) (\x -> x) 5 =
    (\f g x -> f (g (g x))) (\x -> x+2) (\x -> x) 5 = 
    (\g x -> (\x -> x+2) (g (g x))) (\x -> x) 5 =  
    (\x -> (\x -> x+2) ((\x -> x) ((\x -> x) x))) 5 = 
    (\x -> x+2) ((\x -> x) ((\x -> x) 5)) = 
    (\x -> x+2) ((\x -> x) 5) = 
    (\x -> x+2) 5 =
    7 
-}

-- Observe the indentation!    
root w = class
    env = new posix w
    result action
        env.stdout.write (show a ++ "\n")
        env.stdout.write (show (myfun (\x -> x) (\a -> a + 3) 10) ++ "\n") 
        env.stdout.write (show (myfun2 (\x -> x+2) 5) ++ "\n")        
        env.exit 0

