Guido van Rossum schrieb:

>> Another question: is there any intention to
>> support ML style quards for pattern matching on algebraic /recursive 
>> types?
>
>
> Can you rephrase that without references to ML or quads?

No, but the basic idea is easily explained using ML syntax and semantics.

ML lets users define their own datatypes which has the obvious advantage 
that they do not need to pollute their function signatures with 
definitions. Python would likely use class syntax for this purpose or 
interface syntax in future (?)

Some examples:

(* simple 'algebraic datatype' created by RED, YELLOW, BLUE . The vertical bars 
mark alternatives. *)

datatype PrimaryColours = RED | YELLOW | BLUE;  

(* simple recursive datatype. The '*' token is used to combine types to a 
cartesian product type *)

datatype IntTree = NIL | NODE of int * IntTree * IntTree;  


There is a symmetry between the usage of the vertical bars in datatype 
definitions and in "pattern matching" where the pattern mentioned here 
refers to the "algebraic datatype".

(* Function 'colourString' that keeps a PrimaryColours type. Note that ML is 
statically typed and each type will 
   be inferred so declarations in function signatures can be omitted.
 *)

fun colourString c =
    case c of
      RED    => "red"
    | BLUE   => "blue"
    | YELLOW => "yellow"
  ;


(* Another function defined on IntTree *)

fun preOrder t =
    case t of
      NIL => [ ]
    | NODE (i,left,right) => (preOrder left) @ (preOrder right) @ [i]
  ;


Regards,
Kay




_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to