10.07.2015 Views

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

- 140 -7.15. Curried and uncurried functionsIn imperative languages like Pascal and C we use procedures and functions declared with several formal parametersand we cannot separate a procedure or function from the name it is declared with. Here, however, all our functions arebuilt from nested λ functions with single bound variables: names and definitions of name/function associations are justa convenient simplification. Our notation for function definitions and applications has led us <strong>to</strong> treat a name associatedwith a nested function as if it were a function with several bound variables. Now we have introduced another form ofmultiple bound variables through bound variable lists.In fact, nested functions of single bound variables and functions with multiple bound variables are equivalent. Thetechnique of defining multi-parameter functions as nested single parameter functions was popularised by the Americanmathematician Haskell Curry and nested single parameter functions are called curried functions.We can construct functions <strong>to</strong> transform a curried function in<strong>to</strong> an uncurried function and vice versa. For a function fwith a bound variable list containing two bound variables:def curry f x y = f [x,y]will convert from uncurried form <strong>to</strong> curried form.For example, consider:Then for:def SUM_SQ1 [X,Y] = (X*X)+(Y*Y)def curry_SUM_SQ = curry SUM_SQ1the right hand side expands as:so:λf.λx.λy.(f [x,y]) SUM_SQ1 =>λx.λy.(SUM_SQ1 [x,y])def curry_SUM_SQ x y = SUM_SQ1 [x,y]Now, the use of curry_SUM_SQ with nested arguments is the same as the use of SUM_SQ1 with an argument list.Similarly, for a function g with a single bound variable a, which returns a function with single bound variable bdef uncurry g [a,b] = g a bwill convert from curried form <strong>to</strong> uncurried form.For example, withthen for:def SUM_SQ2 X Y = (X*X)+(Y*Y)def uncurry_SUM_SQ = uncurry SUM_SQ2the right hand side expands as:λg.λ[a,b].(g a b) SUM_SQ2 =>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!