lec03 - CS 3110 Lecture 3 Scope, Currying, and Lists Scope...

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

View Full Document Right Arrow Icon
CS 3110 Lecture 3 Scope, Currying, and Lists Scope and binding Curried functions OCaml lists Scope Variable declarations in OCaml bind variables within a scope , the part of the program where the variable standard for the value it is bound to. For example, when we write let x = e 1 in e 2 , the scope of the identifier x is the expression e 2 . Within that scope, the identifier x stands for whatever value v the expression e 1 evaluated to. Since x = v , OCaml evaluates the let expression by rewriting it to e 2 , but with the value v substituted for the occurrences of x . For example, the expression let x = 2 in x+3 is evaluated to 2+3 , and then arithmetic is used to obtain the result value 5 . Functions also bind variables. When we write a function definition in OCaml, we introduce new variables for the function name and for function arguments. For example, in this expression two variables are bound: let f(x) = e 1 in e 2 The scope of the formal parameter x is exactly the expression e1. The scope of the variable f (which is bound to a function value) is the body of the let, e2. A let expression can introduce multiple variables at once, as in the following example: let x = 2 and y = 3 in x + y Here both x and y have the body of the let as their scope. Even though y is declared after x , the definition of y cannot refer to the variable x —it isn't in scope. To declare a recursive function, the function must be in scope within its own body. In OCaml, this requires using a let rec instead of a let . With let rec , every variable it declares is in scope within its own definition and within the definitions of all the other variables. To make this work, all the definitions that use these variables must be functions. For example, here is how we could define mutually recursive functions even and odd : let rec even(x) = (x = 0 || odd(x-1)) and odd(x) = not(x = 0 || not(even(x-1))) in odd(3110) There are two variables named x in this example, both of which are in scope only within the respective
Background image of page 1

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

View Full DocumentRight Arrow Icon
functions that bind them. However, the variables even and odd are in scope in each other's definitions and within the body of the let . Qualified identifiers It is possible to name things defined in a module without using a qualified identifier, using the open expression: # String.length "hi";; - : int = 2 # open String;; # length "bye";; - : int = 3 There are a number of pre-defined library modules provided by OCaml that are extremely useful. For instance, the String module provides a number of useful operations on strings, and the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/25/2009 for the course PHYS 2214 at Cornell.

Page1 / 5

lec03 - CS 3110 Lecture 3 Scope, Currying, and Lists Scope...

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

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