DOC PREVIEW
U of I CS 421 - Lecture Notes Set 3

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 421 – Spring 2007Lecture Notes Set 3:Introduction to OCaml - ContinuedElsa L. Gunter1Transcribed by: Pooja MathurCorresponding to Slides: 01-intro (slides 26-47)Made available: January 22, 2007Revision 1.01 Change Log1.0 Initial Release.2 Basic Procedures2.1 Basic Syntax (slides 26-27)• Use the keyword let to specify that you are introducing the binding of a name• Follow with the name of the procedure• Next is a sequence of arguments (can be a variable, function, can be multiple variables, functions• Then an equal sign• Finally comes the body of the function– Behind the scenes this there is mapping from the fun keyword to the body of the procedure with the part”fun n → n + 2”. Because of this, you do not need to bind a function to a name.So you can use the syntax ”fun [args] →” to write nameless functions. Another example is making a pair out oftwo nameless functions. By doing this, you can pull out either of the two functions at any time and use them. So, youhave a data structure of functions.Functions in Ocaml are first class data, they can be applied, put into data structures, they don’t need names, theyare expressions in their own right, and they can be passed as arguments into another function or even returned fromfunction as you will see later.Remember, when a procedure uses a variable name, the scope of that variable is only for the life of the functionwhile it is running. The environment returns to its former state after the procedure has completed.2.2 An Example (slides 28-31)Here is an example to consider:# let x = 12;;val x : int = 12# let plus x y = y + x;;val plus x : int → int = <fun>1c 2007, Share and Enjoy1The function plus x has one parameter of y. The body of the function is y + x. Before that x had been declaredto be 12. That goes into the environment. So, when running the procedure on 3, that is, plus x 3, you get 15 as youranswer because x is 12 in the environment at the time.# plus x 3;;- : int = 15But what if we redeclare x to be 7.# let x = 7;;val x : int = 7Now, this environment has a new declaration of x, and for any new uses of x, 7 will be used. The old value is stillin the old environment, that environment is just hidden. So note, that does not delete the old value of x, there are twodeclarations of x in the overall environment stack. However, for old uses of the name x, maybe something from theprevious environment, like using plus x again, which x is going to be used? Well, the old environment that containsthe declaration of plus x had 12 as the value. So even though you have hidden 12 from any future use as x (unless youdeclare it as such again) that value is bound to the plus x procedure. So running plus x 3 will still give you the valueof 15.# plus x 3;;- : int = 15In something like C/C++, which uses assignment, plus x 3 would return 10. If you were to do something called”call by name”, you would also get 10. However, OCaml works differently, it is called ”call by value” and you get 15.So overall, when learning any new programming language, you need to figure out, what method of evaluaion doesthis language use? What is the scope of the variable?2.3 Procedures with more than one argument (slides 32-33)To use more than one argument, all you do is add a space and the name of that argument. So in the example, we haveadd three x y z where x, y, and z are all arguments of the function. The type of add three works like this. You haveadd three is a function that takes an integer and returns a function that takes a integer that returns another function thattakes an integer and finally returns an integer.# let add three x y z = x + y + z;;val add three : int → int → int → int = <fun>When the procedure is run on the example, add three 6 3 2, what is happening is add three is run on 6 whichreturns a function. That is then run on 3 which will return another function, and then is finally run on 2 which returnsan integer, the result of 11.You can write something that says add three 6 which actually makes sense in OCaml. If you wrote that you wouldget a function that needed two arguments and had 6 already in there as the original argument. Note that there is nodefault values for any of the arguments, so if you typed add three 6, nothing would happen with the procedure until itreceived its last two arguments.So, if you had a function that took a function and an integer, how do you differentiate between passing in a function”add three 6”, since that is a function and then an integer, as opposed to add three and the number 6 as the integer.The answer is that you use parentheses strategically. Place parentheses around the (add three 6) if you want that to bethe function with another integer as the other argument. Otherwise, it would interpret it as first add three and then 6as another argument.# let h = add three 5 4;;val h : int → int = <fun># h 3;;- : int = 12# h 7;;- : int = 16Now, this example, has add three 5 4 and that returns an function, as we have learned. It then binds h to that newfunction. So when h 3 is run, that actually means something first of all, and then it turns out to be adding 9 to 3, whichis expected, since h is a function that adds 5 + 4 + z. In the first case z is 3. The second time, z is 7 because h 7 wasrun. The result from that is 16.22.4 Passing Functions as Arguments (slide 34)# let thrice f x = f (f (f x));;val thrice : (’a → ’a) → ’a → ’a = <fun>In this example we have a function, thrice which takes a function and an integer and what it does is applies thefunction to the integer and that returns an integer, and then the function is appiled to that value and returns something,and then the function is applied on that value and finally returns it in the end as the final result. Note that the parenthesesare important. Without them, the function won’t be doing what you want it to.When using a function as an argument, you have to make sure you are using it type correctly. That is, it is afunction, so you should treat it like one. It doesn’t make sense to add a function to a function or something along thoselines. And for functions, one way of using it that is type correct is applying it.Note the type of a function passed as an argument. It isn’t a float or an integer or a boolean, OCaml says ’a. Thismeans that you can send in what ever type you want into the thrice will be able to work with …


View Full Document

U of I CS 421 - Lecture Notes Set 3

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download Lecture Notes Set 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes Set 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes Set 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?