Re: Which programming language is better to start




"Jon Harrop" <usenet@xxxxxxxxxxxxxx> wrote in message
news:42bc8515$0$30811$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> >> I'm more interested in the case of a pattern match (because these are
not
> >> native in Lisp). ML does a lot of pattern match optimisations which, I
> >> assume, Lisp cannot do as a consequence.
> >
> > There is no concept of pattern matching on data in Lisp as there is in
> > MLs. These operation would be done by representing the data as lists
> > and applying basic list operations.

Probably not, except in a Lisp class teaching macros...

> There have been whole books written on pattern match optimisations. They
are
> fantastic constructs in terms of simplicity, expressiveness and providing
> great scope for optimisation. The main downside is that dissecting a data
> structure (e.g. lists) using pattern matches ties you to that data
> structure. In contrast, using maps and folds frees you from the underlying
> data structure.
>
> For example, if you have a string match like:
>
> "ABC" -> 1
> | "ABD" -> 2
> | "ABE" -> 3
> | _ -> 0
>
> then you can clearly just lookup the third character in the incoming
string
> and do a little arithmetic (and a branch) to get your answer. Naively
> implementing this requires many branches and many string compares. More
> generally, ML can use hashing (sometimes with optimal hash functions) to
> achieve fantastic performance on much more complicated matches.

Sure. Lisp can use hashing too.

> There are many other optimisations, particularly decision-tree based
> optimisations which can be applied to data structure pattern matches. This
> lets the ML compiler minimise the number of branches required whilst
> guaranteeing the same result.
>
> I had guessed that you could add pattern matches to Lisp but that the Lisp
> compiler could not achieve anything like the performance of ML pattern
> matches.

I still don't see why.


--
Matthieu Villeneuve


.