[Next][Index][Thread]

Re: Gwydion Dylan 2.3.4 Released



On Sat, 30 Dec 2000 11:42:41 -0600, Albert Wagner <alwagner@tcac.net> wrote:
>
> Would someone please post a short definition of imperative,
> functional and OOP languages.  I thought I knew OOP from Smalltalk
> until I read of Dylan's approach.  

Imperative programming: a programming style that uses explicit mutable
state (mutable variables) and control flow (goto, for-loops, branches,
etc.). Conceptually, you perform a computation by changing the state
of the computer. A procedural language adds functions and procedures
on top of this foundation, and then you get C and Pascal.

For example, here's an imperative function to find the factorial of an
integer:

define function factorial(n :: <integer>) => (n! :: <integer>)
  let n! :: <integer> = 1;
  while (n > 0)
    n! := n! * n;
    n  := n - 1;
  end while;
  //
  n! // return n!
end function factorial;

Functional programming: a programming style that emphasizes an
"equational" approach is taken -- instead of changing the state of a
machine, a program is defined as the solution to a set of functions.
In order for mathematical laws to hold, in particular that

  a = b => f(a) == f(b) 

mutable state and explicit control flow are avoided. (For example,
if there were mutable global variables referenced in f, then there
would be no guarantee that one call to f(3) would have the same
value as another call f(3).) 

The usual definitions of functional programming are also that
functions should be first-class, IOW that they can be passed as
arguments and created and returned as values. (Both Smalltalk's blocks
and Dylan's methods are first-class functions.)

Instead, you might define factorial as:

define function factorial(n :: <integer>) => (n! :: <integer>)
  if (n = 0)
    1
  else
    n * factorial(n - 1)
  end if;
end function factorial;

taking the definition of factorial straight from the mathematical
definition.

OOP: A style of programming that emphasizes two concepts. First, the
idea of subtyping -- values can belong to more than one type. Second,
the idea of dynamic dispatch -- a method is selected based on the most
specific type of the arguments. Here's an OO factorial: 

define constant <zero> = singleton(0); // The type <zero> contains only the
				       // int 0, so <zero> subtypes <integer>.

define method factorial(n :: <zero>) => (n! :: <integer>)
  1
end method factorial;

define method factorial(n :: <integer>) => (n! :: <integer>)
  n * factorial(n - 1)
end method factorial;

Note that the factorial method chosen for an argument depends on the
value passed to the function factorial.

The way that Dylan's OO is different from Smalltalk is that rather
than selecting the method based only on the first receiver
(message-passing OO), Dylan selects a method based on *all* of the
arguments (multimethod dispatch). We need two or more arguments to
demonstrate multimethods, and this post has gotten long enough, so
I'll leave that for another day.

> I have just downloaded and installed gd2.3.4, a VERY long compile on
> a 200Mhz machine. Glad to see current dates on NG postings and ftp
> packages.

We have binaries for most platforms -- what do you use?


Neel



References: