[ create a new paste ] login | about

Link: http://codepad.org/rWl7cZNq    [ raw code | output | fork ]

nepta - Lua, pasted on Apr 7:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function f(x)
	return x + 1
end

function g(x)
	return x*x
end

function compose(f,g)
	return function(x)
		return g(f(x))
	end
end


print(compose(f,g)(1)) -- (x+1)(x+1) = (1+1)*(1+1) = 2*2 = 4
print(compose(g,f)(1)) -- (x*x)+1    = (1*1)+1     = 1+1 = 2


Output:
1
2
4
2


Create a new paste based on this one


Comments: