


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Email: [email protected]
Office Hour: Wednesday, 3:30-4:30, lounge.
Thoughts: Please ask me anything you’d like. You will not offend me. I will not think the question is stupid. It is boring to sit in my office hour all by myself.
Wes speaks really quickly. You may get confused. Section should clarify mate- rial, assist with the homework, answer questions, and provide preparation for exams.
Feel free to email me suggestions for how you’d like this section to go, or to ask questions ahead of time (if anything in particular confused you in class, for example).
Different programming paradigms exist: imperative, object-oriented, functional. You are probably basically familiar with the first two. Different languages follow these paradigms, some of which you’ve seen: C, C++, Java, Python, Ruby, LISP, Scheme, OCaml...
Most popular programming languages are imperative – we get stuff done by changing variables, messing around with pointers, etc. Getting the correct out- put at the end is sort of a ’happy coincidence.’ This is not the only way to do things.
In functional programming, we are concerned with the evaluation of mathemat- ical functions. The function is the first-class citizen. Importantly, functional programming avoids mutable variables and and direct modication of the state. This may seem counter intuitive (and counter productive, if youre working on PA1...). Nevertheless, functional programming is just as powerful as imperative programming. In other words, there is nothing you can do with C that you cant do with OCAML...
Time for an illustrative example! Consider this exciting piece of code:
x = x + 5
Suppose this is valid code in some imperative language (say C), and some func- tional language (like OCAML or Haskell). This is what happens:
C: The value x + 5 is computed. Variable x is set equal to the result of this computation (the original value of x is replaced).
Some Functional Language: Infinite recursion! Symbol x = x + 5 = (x +
(In general, though, we use recursion (of the non-infinite variety) to get stuff done in functional programming languages.)
Often, the best way to learn is to do. Time for more examples! Lets do some folding in OCAML:
mymachine:/cs415 ocaml Objective Caml version 3.09.
val odd: int -> bool =
val add : int -> int -> int =
val sum : int list -> int =
(for the next element), or it can be an instance of Nil, which always ends the list.
Lets have some practice with this list datatype. Well add an append method. Instead of insert (which does an insertion sort), append will always add its element to the end of the list.
Class List inherits IO { -- ... append(i : String) : List { self }; }; Class Nil inherits List { -- Nil is an empty list -- ... append(i : String) : List { (new Cons).init(i, self) }; }; Class Cons inherits List { -- a Cons cell is a non-empty xcar : String; -- list xcdr : List; -- ... append(i : String) : List { (new Cons).init(xcar, xcdr.append(i)) }; };
Note that our append method is recursive. For an empty list (consisting of just one Nil object), the method returns a new pair. The new list consists of a single pair that has a reference to the string and to the original Nil object, which now serves as the end-of-list marker.
For any nonempty list, the append method will have to traverse to the end of the list. This is done by creating a new cons cell at every step. The new cell has the same element, but its tail is updated with a recursive call to append. Eventually this call chain will reach a Nil object.
This list data structure is very inefficient. Appending an element to the end of the list takes O(n) operations, and it creates a whole new set of cons cells. Note to self: how much time would it take to prepend an element (i.e. tack it to the front of the list rather than the back)?