[plt-scheme] Re: idiomatic way to split a list into first, middle, last?

From: Jean-Paul.ROY at unice.fr (Jean-Paul.ROY at unice.fr)
Date: Fri Jan 6 10:14:27 EST 2006

I think the Mathematica language has a (non linear !) pattern- 
matching very interesting :

(* defining function split, notice 1 underscore for element pattern  
x_ and y_
    and 2 underscores for segment pattern y__ *)
split[{x_, y__, z_}] := {x, z}

(* using split *)
In[8]:= split[{a, b, c, d, e, f}]
Out[8]= {a, f}

(* defining function find to get the list after the second occurence  
of x *)
find[{x_, y__, x_, z__}] := z

(* using find *)
In[10]:= find[{a, b, c, d, a, e, f, g}]
Out[10]= Sequence[e, f, g]

Alas, no interpreter source of the Mma language is available (except  
some hack by Richard Fateman in Lisp I think)...
Such a match functionality in DrScheme could maybe even improve over  
ML matching ?


Posted on the users mailing list.