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: Andrew Suffield
Subject: Re: [Gnu-arch-users] Re: Tla spork
Date: Fri, 27 Aug 2004 16:50:10 +0100
User-agent: Mutt/1.5.6+20040803i

On Fri, Aug 27, 2004 at 04:40:37PM +0200, Tobias C. Rittweiler wrote:
> (define (fib n)
>   (if (or (= n 0) (= n 1))
>      1
>      (+ (fib (- n 1)) 
>         (fib (- n 2)))))

Now, let's introduce a rule that says parentheses aren't required:
function applications bind stronger than anything else and associate
to the left.

define (fib n)
  if (or (= n 0) (= n 1))
    1
    + (fib (- n 1))
      (fib (- n 2))

That's got rid of a few nasty parentheses.

Now introduce a rule that if a function name is wrapped in (), it is a
prefix function, and if it is wrapped in ``, it is an infix
function. Currently, all functions are prefix by default. We'll
introduce normal logical precedence at the same time.

Now we introduce a rule which says that a function with a name
comprised of punctuation !#$%&*+./<=>address@hidden|-~ is infix by default.

define (fib n)
  if (or ((=) n 0) ((=) n 1))
    1
    (+) (fib ((-) n 1))
        (fib ((-) n 2))

But we can also have:

define (fib n)
  if (or (n = 0) (n = 1))
    1
    fib (n - 1) + fib (n - 2)

And even:

define (fib n)
  if (n = 0 `or` n = 1)
    1
    fib (n - 1) + fib (n - 2)

But let's introduce operator ||:

define (fib n)
  if (n = 0 || n = 1)
    1
    fib (n - 1) + fib (n - 2)

Rename = to ==:

define (fib n)
  if (n == 0 || n == 1)
    1
    fib (n - 1) + fib (n - 2)

Now, introduce operator '=' which is the infix form of 'define':

fib n = if (n == 0 || n == 1)
          1
          fib (n - 1) + fib (n - 2)

Now we're starting to get something that's nowhere near as ugly. Let's
change 'if' to have 'then' and 'else' delimiters:

fib n = if n == 0 || n == 1
          then 1
          else fib (n - 1) + fib (n - 2)

There, you don't even need to know the language to understand what
that means. But we're not done yet. if/then/else is a terrible
structure for non-trivial purposes; it lends itself to unnecessary
levels of indentation. Let's introduce 'guards'. They work like this:

 | a = b
 | c = d
 otherwise = e

Which means:

 if a then b else (if c then d else e)

(The list of guards can extend without limit)

And now we have:

fib n
 | n == 0  = 1
 | n == 1  = 1
 otherwise = fib (n - 1) + fib (n - 2)

Now *that* is a real syntax (it's Haskell). Almost all the nasty
parentheses have been eliminated, and those that remain merely enhance
clarity rather than reduce it.

-- 
  .''`.  ** Debian GNU/Linux ** | Andrew Suffield
 : :' :  http://www.debian.org/ |
 `. `'                          |
   `-             -><-          |

Attachment: signature.asc
Description: Digital signature


reply via email to

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