gnu-arch-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gnu-arch-users] Re: Tla spork


From: Robert Collins
Subject: Re: [Gnu-arch-users] Re: Tla spork
Date: Mon, 30 Aug 2004 23:37:45 +1000

On Fri, 2004-08-27 at 16:40 +0200, Tobias C. Rittweiler wrote:
> On Friday, August 27, 2004 at 7:10:47 AM, 
>     Zenaan Harkness <address@hidden> wrote:

> 
> fib(n)
> {
>   if ((n = 0) || (n = 1))) {
>     return 1;
>   } else {
>     return fib(n-1) + fib(n-2);
>   }
> }

just for kicks, here is a fib example in smalltalk, a classic OOP
language.

!Integer methodsFor: 'demonstration'!
fib
  ^self <= 1
    ifTrue: [1]
    ifFalse: [(self - 1) fib + (self - 2) fib]
!!

this is the 'opposite' of the oop/functional spectrum if you like. (I
really need to play with ocaml soon).

For strangers to the syntax, the above breaks down like:
'demonstration' is a string literal
!Integer methodsFor: 'demonstration'!
this introduces the addition of methods to the class Integer.

fib
this is the method signature. in this case its a unary (no arguments)
method called 'fib'.

^self <= 1
^ - this indicates that we return the result of evaluating the
expression.
self - 'this' in C++. usually 'self' in python, but its not actually
defined to be anything specific.
<= - this is a binary message.
1 - numeric literal

smalltalk doesn't delimit expressions by whitespace, so the expression
continues to the next line. the precedence rules of smalltalk (unary,
binary, keyword), mean that when the next line is being evaluated, self
<= 1 has already been evaluated, which will be either true or false.

the next two lines are a single method call to the Boolean instance that
<= created. the method is ifTrue:ifFalse:, each part of which takes a
code block to evaluate if it matches. two more bits of syntax are needed
to read the lines. [] indicates a code block (a lambda), () are
brackets, and do what you'd expect - enforce evaluation order.

ifTrue: [1] 
ifFalse: [(self - 1) fib + (self - 2) fib] 

these then are the two branches needed for the recursion. 
The !! indicates the end of method creation.

Rob

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]