Re: Scripting with Visual Basic Clone


Subject: Re: Scripting with Visual Basic Clone
From: Aaron Lehmann (aaronl@vitelus.com)
Date: Thu Jun 29 2000 - 17:52:16 CDT


On Thu, 29 Jun 2000, Tomasz Wegrzanowski wrote:

> > Scheme Syntax in a few simple rules.
> > - -----------------------------------
> >
> > 1) All expressions are surrounded by parenthesis.
> > Example: (func arg1 arg2)
>
> People are used to surround arguments by parenthesis.
> (f x) looks strange comparing with f(x).

Obviously, before you program in a language it is wise to learn it. It may
look strange to you becuase you don't know the language, just as perl
looks more than strange to me becuase I have not memorized the dozens of
special characters that make up its syntax.

> > 2) Everything is prefix.
> > Example (+ 1 2) - this evaluates to 3
>
> Math is most important kind of simple scripting.

Since when? My scripts for AbiWord would not be crunching quadratics or
anything remotely more mathematical than counting or multiplication.

> It is important for script-math to look more or less like real math.
> If one needs to count:
> sqrt(b*b-4*a*c)-b -sqrt(b*b-4*a*c)-b
> ----------------- and ------------------
> 2a 2a
> In most languages he only need to know how to change it to flat expresion.
> `(sqrt(b*b-4*a*c)-b)/2', which is still mathematically-correct,
> so he will probably do this without help, and know how to change
> math sqrt symbol to sqrt().

Prefix

> In scheme :
>
> (/ (- (sqrt (- (* b b) (* (* 4 a) c))) b) (* 2 a))
> (/ (+ (sqrt (- (* b b) (* (* 4 a) c))) b) (* 2 a))

(/ (+ (sqrt (- (* b b)
               (* 4 a c)))
      b)
   (* 2 a))

... is a much nicer way to format it.

You forgot one of the advantages of postfix/prefix notation. Usually, this
type of math does not have any parentheses. This is of course not true in
Scheme. But the advantage that using parentheses has is that you can pass
a variable number of arguments to an operator. I replaced your
(* (* 4 a) c) with (* 4 a c), and they are equivilent.

Here's the way I'd implement the quadratic formula in scheme, which is a
far out example since I can't imagine it being used in a simple script for
a word processor:

(define quad (lambda (a b c)
             (let ((discriminant (sqrt (- (* b b)
                                          (* 4 a c)))))
               (list (/ (- discriminant b)
                        (* 2 a))
                     (/ (+ discriminant b)
                        (* 2 a))))))

You wanted to compete with mathematics, so let's do that:

> (quad 1 2 3)
(1.0-1.4142135623730951i 1.0+1.4142135623730951i)

See how it even handles complex numbers? Just try that in C without
trickery!

> Is this any similar to real math ?
Moreso than C or any other non-lisp languages (other than Mathematica)
that I know that don't handle complex numbers or Bignums.
> Is this correct ?
It's more correct than it would be in C, as in C you would probably get an
error or an incorrect answer if the correct answer was complex.
> It's easier to make a uncorrectable (unfindable) typo in such expression
> that to make a sigsegv in C.
Not when you have blink-parens-mode :).

> People simply don't think prefix.
> Sure, one can parse single-() in his head, but unless you have
> different types of parens on display or many-months LISP experience,
> you won't be even able to parse expressions above in reasonable time.

It would be a lot easier to parse something like:

sqrt(b*b-4*a*c)-b -sqrt(b*b-4*a*c)-b
----------------- and ------------------
        2a 2a
Than:
(sqrt(b*b-4*a*c)-b)/2*a

But I don't know of any language that will let you use the former in a
program.

> > 3) Bind some name to some value with define.
> > Example: (define pi 3.14159)
> > This works for functions too.
> >
> > 4) Return a function with lambda. Arguments come after the lambda.
> > Example: This returns a function that squares its argument.
> > (lambda (x)
> > (* x x))
>
> And lambda calculus is of course so widely known.

As I said, you shouldn't know a programming language before you learn it!
Learning what a lambda expression is is about as hard as understanding a C
or Perl function.

> Functions aren't part of newbie's scripts anyway.

My scripts for emacs are chock-full-of-defuns. And I consider myself quite
an emacs newbie and a lisp newbie.

> > 5) Conditionals with cond
> > (cond
> > (test1) (exp1)
> > (test2) (exp2))
>
> Two tests in one conditional ?
> Conditionals always takes one test.
Not in scheme. (cond) is similar to C's 'switch' statement or Pascal's
'CASE' statement. It's just the name.

> > 6) Create a literal with '
> > Example '(1 2 3) returns (1 2 3)
>
> Unbalanced quotes. Y'know how people react for it.

I fail to see what's so hard about unballanced quotes. It takes about 5
minutes to get used to.

> > These are the basic rules of Scheme syntax. There are important, more
> > complicated, and very useful additons, but lots can be done with these,
> > and the following primitive (defined for you) functions.
> >
> > first - returns the first part of a list (first '(a b c)) -> a
> > rest - returns the list without the first part (rest '(a b c)) -> (b c)
> > and, or, equal? - self-explanatory
> > +, -, / - integer ops
> > display - outputs args to screen eg (display (first '(1 2 3)))
> >
> > I challenge anyone to reduce another language to as few rules. (other
> > LISP derivatives excluded).
>
> As few syntactic rules ?
> I take a challenge.
>
> Look at you-favourite-RISC-machine assembler.
> EBNF for it looks like :
> ([label: ] operation [argument1 [argument2]] )*
>
> Number of rules have more to do with complexity of parser
> that with complexity of language.
> C and bash have less tokens than Perl and Python.

C is a hell of a lot simpler than Perl, as is bash. I don't know Python.

> Are they simpler because of this ?
>
> Maximally simple real language is Smalltalk.
>
> Its FULL syntax takes a few display pages,
> but I don't have them here to show you.
> It is probably more than SCHEME/*LISP, but these rules make
> it much better looking and way easier to undestand :
>
> ---start
> 4 squared.
> 5 "cats" * 2 "mice per cat" * 50 "gram of cheese per mice".
> '2 + 2 * 2 = ' print.
> "It is 8" 2 + 2 * 2.
> '2 + 2 squared = ' print.
> "It is 6" 2 + 2 squared.
> ---end

Looks like an interesting language.

>
>



This archive was generated by hypermail 2b25 : Thu Jun 29 2000 - 17:52:26 CDT