Task

Create a function, compose,   whose two arguments   f   and   g,   are both functions with one argument.



The result of compose is to be a function of one argument, (lets call the argument   x),   which works like applying function    f    to the result of applying function    g    to    x.



Example

 compose(f, g) (x) = f(g(x))



Reference: Function composition

Hint: In some languages, implementing compose correctly requires creating a closure.




sequence ctable = {}
 
function compose(integer f, integer g)
    ctable = append(ctable,{f,g})
    return length(ctable)   
end function
 
function call_composite(integer f, atom x)
integer g
    {f,g} = ctable[f]
    return call_func(f,{call_func(g,{x})})
end function
 
function plus1(atom x)
    return x+1
end function
 
function halve(atom x)
    return x/2
end function
 
constant m = compose(routine_id("halve"),routine_id("plus1"))
 
?call_composite(m,1)    -- displays 1
?call_composite(m,4)    -- displays 2.5
