Docsity

Discussion Section Notes - Programming Languages | CS 4610, Study notes of Programming Languages

Toggle sidebar
bg1
CS 415 – Discussion Section Notes 1
Claire Le Goues
January 31, 2008
1 Talk to me!
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.
2 Why Section?
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).
3 Functional Programming
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.
1
pf3
pf4

Partial preview of the text

Download Discussion Section Notes - Programming Languages | CS 4610 and more Study notes Programming Languages in PDF only on Docsity!

CS 415 – Discussion Section Notes 1

Claire Le Goues

January 31, 2008

1 Talk to me!

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.

2 Why Section?

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).

3 Functional Programming

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 +

    • 5 = ((x + 5) + 5) + 5, etc. Each time the interpreter sees x, it treats it as a mathematical symbol. The expression diverges.

(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.

open List (* for fold_left, map, filter, etc. *) ;;

let odd x = x mod 2 != 0 ;;

val odd: int -> bool =

let add x y = x + y ;;

val add : int -> int -> int =

let sum = List.fold_left add 0 ;;

val sum : int list -> int =

sum [1;2;3];;

  • : int = 6

sum (filter odd [1;2;3;4;5;6;7;8;9]);;

  • : int = 25

sum [1;3;5;7;9];;

  • : int = 25

(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)?