0% found this document useful (0 votes)
40 views

Programming Languages Dan Grossman 2013: Nested Functions

The document discusses nested functions in programming languages. It provides an example of defining a countup_from1 function that uses a nested count function to recursively count from 1 to the input parameter x. A better version is shown that removes an unnecessary parameter from the nested count function. The document advises that it is good style to define helper functions nested within other functions if they are unlikely to be useful elsewhere, at risk of misuse if exposed, or likely to change. This captures a fundamental tradeoff between reusing code for efficiency versus flexibility to change code.

Uploaded by

Teja Kamal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Programming Languages Dan Grossman 2013: Nested Functions

The document discusses nested functions in programming languages. It provides an example of defining a countup_from1 function that uses a nested count function to recursively count from 1 to the input parameter x. A better version is shown that removes an unnecessary parameter from the nested count function. The document advises that it is good style to define helper functions nested within other functions if they are unlikely to be useful elsewhere, at risk of misuse if exposed, or likely to change. This captures a fundamental tradeoff between reusing code for efficiency versus flexibility to change code.

Uploaded by

Teja Kamal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Languages Dan Grossman 2013

Nested Functions

Any binding
According to our rules for let-expressions, we can define functions inside any let-expression let b1 b2 bn in e end

This is a natural idea, and often good style

Jan-Mar 2013

Dan Grossman, Programming

(Inferior) Example
fun countup_from1 (x : int) = let fun count (from : int, to : int) = if from = to then to :: [] else from :: count(from+1,to) in count (1,x) end

This shows how to use a local function binding, but: Better version on next slide count might be useful elsewhere

Jan-Mar 2013

Dan Grossman, Programming

Better:
fun countup_from1_better (x : int) = let fun count (from : int) = if from = x then x :: [] else from :: count(from+1) in count 1 end

Functions can use bindings in the environment where they are defined: Bindings from outer environments Such as parameters to the outer function Earlier bindings in the let-expression Unnecessary parameters are usually bad style Like to in previous example
Jan-Mar 2013 Dan Grossman, Programming 4

Nested functions: style

Good style to define helper functions inside the functions they help if they are: Unlikely to be useful elsewhere Likely to be misused if available elsewhere Likely to be changed or removed later A fundamental trade-off in code design: reusing code saves effort and avoids bugs, but makes the reused code harder to change later

Jan-Mar 2013

Dan Grossman, Programming

You might also like