CSC/ECE 517 Fall 2012/ch1 1w32 cm

From Expertiza_Wiki
Revision as of 02:29, 13 September 2012 by Mptapasw (talk | contribs) (Created page with " '''CLOSURES FOR STATICALLY TYPED LANGUAGES ''' == '''INTRODUCTION''' == A closure is a function or reference to a function together with a referencing environmen...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
       CLOSURES FOR STATICALLY TYPED LANGUAGES    

INTRODUCTION

A closure is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables) of that function.[1] A closure—unlike a plain function pointer—allows a function to access those non-local variables even when invoked outside of its immediate lexical scope.Closures can typically be treated like any other programming language objects, e.g. they can be stored in variables, passed to functions, and so on. The best way to understand closures is to think about an example in Scheme.

(define f (lambda (x)

(lambda (y)

(+ x y))))

This is the simplest non-trivial example using closures. Here f is a function of one argument (x). When you call it, you pass it a single number as an argument, which gets bound to x. The return value from calling the function is another function which takes one argument (lambda (y) ...) This new function always adds x to its input (whichever x was passed when the function was created). [3]