Lecture 02

Today:

    1. Reading questions from Louden.

    2. In-class part of Homework 1.

For next time:

    1. Read Louden, Programming Languages, Chapter 1, Sections 1.4 through 1.6 and answer the reading questions below.

    2. Finish Homework 1.

Higher-order functions

map, filter and reduce are examples of higher-order functions; that is, functions that take functions as arguments.

This sort of thing is common in languages like Lisp and Python; less common in the C family languages, but can be implemented fairly easily with function pointers. Anyone know how this is done in Java?

<aside>

Interesting article about the implementation of map, filter and reduce in Python:

http://www.artima.com/weblogs/viewpost.jsp?thread=98196

This article provides insight into the decision process of a language designer.

</aside>

The other feature of higher-order functions is that they often return functions. Easy in Scheme, etc. Hard in C family.

Here's a contrived example:

(define (incrementor n)

(lambda (x) (+ x n)))

And here's how you would use it:

> (incrementor 1)

#<procedure>

> ((incrementor 1) 2)

3

Notice that the anonymous function created by the lambda has a reference to n, which is defined in the enclosing scope.

When this function is applied later, it needs to be able to refer to n.

How do you think that works? Hint: http://en.wikipedia.org/wiki/Closure_(computer_science)

List-tree ambiguity

What is the value of

(cons (list 1 2)

(list 3 4))

Draw a box and pointer diagram and then interpret it.

You can think of a list of lists as a tree, but not necessarily the other way around.

Reading questions

Louden, Programming Languages, Chapter 1, Sections 1.4 through 1.6

1) What is the difference between syntax and semantics?

[Helpful Note: a grammar is a set of rules that describes a language, which is a set of strings (in the case of a programming language, it's the set of syntactically legal programs).]

2) Why is it hard to write language semantics in a natural language? What is the alternative?

3) What's the difference between an interpreter and a compiler?

4) What is a pseudointerpreter?

5) What are the usual stages of language translation?

6) Give an example of a static property of a program. Give an example of a dynamic property.

7) What does it mean to say that a language is "an interpreted language"?

8) What are the different kinds of errors?

9) What is a pragma?

10) What are some of the characteristics of a good programming language?