CPSC 401. Class 20

Tests should be ready by Tuesday
Discuss topics from Chapter 16

  • classes
  • prototypes
  • inheritance
  • encapsulation
  • polymorphism

Another view, from Wikipedia entry fro Object Oriented, http://en.wikipedia.org/wiki/Object_oriented#Fundamental_features_and_concepts

“Benjamin C. Pierce and some other researchers view as futile any attempt to distill OOP to a minimal set of features. He nonetheless identifies fundamental features that support the OOP programming style in most object-oriented languages:[14]

  • Dynamic dispatch – when a method is invoked on an object, the object itself determines what code gets executed by looking up the method at run time in a table associated with the object. This feature distinguishes an object from an abstract data type (or module), which has a fixed (static) implementation of the operations for all instances. It is a programming methodology that gives modular component development while at the same time being very efficient.
  • Encapsulation (or multi-methods, in which case the state is kept separate)
  • Subtype polymorphism
  • Object inheritance (or delegation)
  • Open recursion – a special variable (syntactically it may be a keyword), usually called this or self, that allows a method body to invoke another method body of the same object. This variable is late-bound; it allows a method defined in one class to invoke another method that is defined later, in some subclass thereof.”

Two examples  of dynamic dispatch

 

What are desirable characteristics of a programming language?

 

Java Tutorial on Exceptions.

Videos:

A few examples of exception handling

Handout with exception examples.

For next class:

Posted in class | Leave a comment

CPSC 401. Class 17

Chapter 14. Memory management

A programming assignment – Due March 22.

Do exercises 1 and 2 on pages 265-267 of the text.

Chapter 15

Consider two related techniques in Java

Posted in class | 2 Comments

CPSC 401. Class 16

Quiz

Seven Languages panel

Chapter 14. Memory management

A programming assignment – Due March 22, 10 days from today.

Do exercises 1 and 2 on pages 265-267 of the text.
 

Posted in Uncategorized | 7 Comments

CPSC 401. Class 15

Chapter 12 – Memory Locations for Variables

Activation records – see pages 186, 188-193 (PowerPoint starting with Slide 24)

Now consider dealing with nested functions.

Take a look at how this is handled in Quicksort,

See diagram on page 196 (PowerPoint starting with Slide 32)

Rules on page 196

  1. When calling a top-level function, set the nesting link for the called function to NULL
  2. When calling from a top-level function to a nested function, set the nesting link to the address of the caller’s activation record.
  3. When calling from a nested function to a nested function, set the nesting link for the called activation the same as the caller’s nesting link.

Need to consider functions as parameters.

fun addXToALL (x, theList) =
   let
      fun addX y =
           y + x;
      in
          map addX theList
    end;

we can’t use rule 2 above when we make the call map addX theList, because x is not resolved that way.
We need to view a function value as the implementation along with the nesting link. See pages 199 and 200. (PowerPoint slides 41 – 45)

Long-lived functions

Consider

fun fonToAddX x =
   let
      fun addX y =
           y + x;
      in
         addX
    end;
fun test () =
  let
     val f = funToAddX 3;
  in
     f 5
end;

How do we handle this? See page 201 and 202. (PowerPoint Slide 47 – 53)

If time try exercises 2,3 page 203

We now move the object-oriented paradigm for programming languages. Weber uses Java as an exemplar.

Object-Oriented Programming, defined in Wikipedia

Object-oriented programming (OOP) is a programming paradigm using “objects” – data structures consisting of data fields andmethods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstractionencapsulationmessagingmodularitypolymorphism, and inheritance. Many modern programming languages now support OOP.

Since you all now Java we will jump to Chatper 14, Memory Management to get you ready for a programming assignment – Due March 22, 10 days from today.

Do exercises 1 and 2 on pages 265-267 of the text.

Posted in class | Leave a comment

CPSC 401. Class 13.

For next time, read Can Your Programming Language Do This?
by Joel Spolsky
so that you can discuss it in class. Think of two or three questions you have while reading it, write them down, and post them as comments to this blog post.

Notes via power point.

 

Pages 144 – 147.  Each of these can be done as a one-line solution using  map, foldr, and foldl.

Do as many as you like from exercises 1- 13

Do as many as you like from exercises 14- 25

 

Understand
fun cubelist L = map (fn x => x*x*x) L;

fun cubesum L = foldr (fn (x,y) => x*x*x + y) 0 L;

Comments about namespaces and scope

Posted in class | 1 Comment

CPSC 401. Class 11

fun insertsort [] = []
 | insertsort (x::xs) =
 let fun insert (x, []) = [x]
 | insert (x, y::ys) =
 if x<=y then x::y::ys
 else y::insert(x, ys)
 in insert(x, insertsort xs)
 end;

Trace the program for

val urList = [ 3.0, 5.0,12.78, 1.89, 2.0 + 6.0, 2.0 * hd(urList),  9.0/10.0];

 

Any polymorphism going on here?

fun appendString(x:'a,s: string, toString:'a-> string) : string
= (toString x) ^ " "^ s;

 

some examples:

appendString(401, "class", Int.toString)
appendString("three", "twelve", fn(s:string) => s)

Now consider the type IntList defined as

datatype IntList = Nil | Cons of int * IntList;

not bad for a ‘wrapper’ type but we can make this more general

datatype ‘a List = Nil | Cons of   ‘a * ‘a List;

Is this what the author  calls parametric  polymorphism?

Future of Programming Languages. Part I – stop when Tate starts talking about his love affair.

Chapter 9 –  More ML

For next time, read Can Your Programming Language Do This?
by Joel Spolsky
so that you can discuss it in class. Think of two or three questions you have while reading it, write them down, and post them as comments to this blog post.

Notes via power point.

 

Pages 144 – 147.  Each of these can be done as a one-line solution using  map, foldr, and foldl.

Do as many as you like from exercises 1- 13

Do as many as you like from exercises 14- 25

 

Understand
fun cubelist L = map (fn x => x*x*x) L;

fun cubesum L = foldr (fn (x,y) => x*x*x + y) 0 L;

Comments about namespaces and scope

 

Posted in class | 17 Comments

Seven Languages Blogs

  • Erlang
    • Blog – Erlang
    • Members
      • Amanda Jenkins
      • Phillip Thomas
      • David woodruff
      • Brian Odonnel
  • Clojure
    • Blog – Clojure
    • Members
      • Rebecca Zeitz
      • Christian Karrs
      • Kaylee Wichert
      • Ned Drummond
  • Haskell
  • Io
    • Blog - I Know Io | Learning how to use Io in three days
    • Members
      • Marlene Logan
      • JoJo Wang
      • Franklin Lee
      • John Corrigan
  • Scala
    • Blog – Scala
    • Members
      • Ashby Bowles
      • kevin Groat
      • Jacob Bowman
      • Gray Kemmey
  • Ruby
  • Prolog
    • Blog – Prolog Journal.
    • Members
      • Joe Proffit
      • Matt Miller
      • Matt Martin
Posted in seven languages | Tagged , , , , , , | Leave a comment

CPSC 401. Class 9

Chapter 7 topics.

Pattern Matching

  • See section 7.5 for  a summary of ML patterns (so far)
  • Pattern Matching used in function definitions. Rewrite the functions from the last quiz.

Local Variable definitions
Use let expression

<let-exp> ::= let <definitions> in <expression> end

fun days2ms days =
let
   val hours = days * 24.0
   val minutes = hours * 60.0
   val seconds = minutes * 60.0
in
    seconds * 1000
end;

Take a look at fun halve on page 111

fun halve nil = (nil, nil)
|   halve [a] = ([a], nil)
|   halve (a :: b :: cs) =
      let
        val (x, y) = halve cs
      in
        (a :: x, b :: y)
      end;

Examine halve [42];
halve[41,43];
halve[ 1,45,23,13];

Now look at merge

fun merge (nil, ys) = ys
|   merge (xs, nil) = xs
|   merge (x :: xs, y :: ys) =
      if (x < y) then x :: merge(xs, y :: ys)
else y :: merge(x :: xs, ys);

merge ([13,56], [14, 18, 66])

 

Finally lets look at mergesort
Chapter 8 Topics
Polymorphism

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions. A function that can evaluate to or be applied to values of different types is known as a polymorphic function. A data type that can appear to be of a generalized type (e.g., a list with elements of arbitrary type) is designated polymorphic data type like the generalized type from which such specializations are made. – from Type Polymorphism,

A function or operator is polymorphic if it has at least two types.

Overloading

For example + in ML, C++, Java

Here’s another

float perimeter( float width, float height ) { return 2*(width + height);}
float perimeter (float  sidea, float sideb, float sidec){ return sidea+sideb+sidec;}

Coercion int i = 23.8 +2; // implicit coercion
explicit coercion.
What coercions are allowed by the language? See pages 122 123.
Ad Hoc Polymorphism

  • A function or operator exhibits ad hoc polymorphism if it has at least two but only finitely any types
    • Overloading
    • Parameter Coercion

Universal polymorphism
A function or operator exhibits universal polymorphism if it has infinitely many types
Parametric Polymorphism
Subtype Polymorphism

Posted in class | Leave a comment

100-year language writing assignment

  • Writing assignment for Thursday, February 23, 2012 (by 5PM).
  • Read
    The article “The Hundred-year Language.” Here is a bibliographic reference to that article in ACM style.
    GRAHAM, P. 2003. The Hundred-Year Language http://www.paulgraham.com/hundred.html
    The article raises some interesting questions. I’ve grouped a few below. Choose one group and write an essay that considers the questions by surveying some of the literature on the topic, stating what others have said about an answer to that or a closely-related question. Your essay should include a conclusion based on your research. While I am often interested in your opinion, this essay isn’t the place for it. I’d like you to write this using ACM style for citations and the list of items cited. Cite at least 3 works (no encyclopedias). The essay should be between 500 and 1000 words, and written for an audience that consists of upper-level students majoring in computer science. Let me know if you have questions. (You might also want to read “The Future of Programming: An Interview with PaulGraham” in the Spring 2006 issue of ACM Crossroads.)

    1. Will we even be writing programs in a hundred years? Won’t we just tell computers what we want them to do?
    2. How will we take advantage of the opportunities to waste cycles that we’ll get from new, faster hardware?
    3. How far will this flattening of data structures go? I can think of possibilities that shock even me, with my conscientiously broadened mind. Will we get rid of arrays, for example? After all, they’re just a subset of hash tables where the keys are vectors of integers. Will we replace hash tables themselves with lists?
    4. Could a programming language go so far as to get rid of numbers as a fundamental data type?
    5. The real question is, how far up the ladder of abstraction will parallelism go? In a hundred years will it affect even application programmers? Or will it be something that compiler writers think about, but which is usually invisible in the source code of applications?
    6. Mr. Graham states: “Inefficient software isn’t gross. What’s gross is a language that makes programmers do needless work. Wasting programmer time is the true inefficiency, not wasting machine time. This will become ever more clear as computers get faster.’ Is it the case that optimization of code is not something that programmers should be concerned with?

    Timetable for this assignment.

    • For Thursday, February 16: Choose a question, come up with three resources, and an outline of what you will write about. Bring it to class.
    • For Thursday, February 23. Submit two drafts. The final and the most recent draft before the final draft. Identify each
Posted in assignments | Tagged , | 16 Comments

CPSC 401. Class 8

Quiz on Chapters 5 and 6.

Answer these questions:

  • What is purpose of  type checking?
  • When are two types equivalent?
  • What does the author mean when he says that a key question for type systems is how much of the representation is exposed?
  • Are the types bool and list primitive types in SML?

A few other lectures about types you may want to look at:

Pattern matching in ML

Posted in class | Leave a comment