324-lecture10 - Principles of Programming Languages...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Principles of Programming Languages Principles of Programming Languages Lecture 10 Lecture 10 University of Toronto Wael Aboulsaadat Wael Aboulsaadat wael@cs.toronto.edu wael@cs.toronto.edu http://portal.utoronto.ca/ 1 SC324: Principles of Programming Languages cheme: Functions cheme: Functions The general form of a Scheme function is ( define (<name> <formal parameters>) (<body>)) Examples: ( define (square x) (* x x)) University of Toronto ( define (sum-of-squares x y) (+ (square x) (square y) ) ) ( define (circle-area rad) (* 3.14 (square rad) ) ) 2 SC324: Principles of Programming Languages cheme: Functions cheme: Functions Scheme functions are first-class objects: Can be created dynamically Can be stored in data structures, Can be returned as results of expressions or procedure. This means that a scheme program can evolve its behavior as it runs! This means that a scheme program can evolve its behavior as it runs! University of Toronto 3 SC324: Principles of Programming Languages cheme: Pure Functions cheme: Pure Functions A pure function is one that simply uses its input arguments to compute a return value, without performing any side-effects.? Side-effects are changes to the systems computational state that could affect future calls to itself or other functions. In a language that only uses pure functions, any function call with stantiated arguments, e.g. (f 5 10), ALWAYS returns the same University of Toronto instantiated arguments, e.g. (f 5 10), ALWAYS returns the same value and hence means the same thing in the context of a particular program. The absence of side effects makes it much easier to formally analyze the behavior of a system, since: We can reason about the system in terms of independent function calls, without having to worry about the (side) effects of these calls on future calls. We can simplify the code accordingly. E.g. (+ (f 5 10) (f 5 10)) simplifies to (* 2 (f 5 10)) for any numeric function f 4 SC324: Principles of Programming Languages cheme: Pure Functions cheme: Pure Functions contd contd Referential Transparency: syntactically identical expressions mean the same thing, (i.e. return the same result when evaluated) regardless of WHERE they appear in a program....
View Full Document

This document was uploaded on 08/02/2011.

Page1 / 19

324-lecture10 - Principles of Programming Languages...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online