Penguin

Differences between version 16 and predecessor to the previous major change of ML.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 16 Last edited on Monday, February 24, 2003 3:22:18 pm by GlynWebster Revert
Older page: version 15 Last edited on Thursday, February 20, 2003 5:04:31 pm by GlynWebster Revert
@@ -43,34 +43,9 @@
  
 [2] 
 !!!Polymorphism 
  
-This is what prevents ML's strong type checking from being a pain in the bum. You don't have to define the type of everything ''exactly'', you can leave some types, or parts of some types unspecified. For example this is a type for binary trees:  
-  
- # __type__ 'a tree = Leaf __of__ 'a | Node __of__ 'a tree * 'a tree ;;  
- ''type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree''  
-  
-(That's me typing at the Ocaml interpreter prompt and Ocaml responding.) The type __tree__ is polymorphic: __'a__ is a place holder for any type. For example you might want a __string tree__:  
-  
- # __let__ my_tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat") ;;  
- ''val my_tree : string tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat")''  
-  
-The Ocaml interpreter was in fact clever enough to work that was a string tree by itself. From my tree type I can make __int trees__, __char trees__, __string tree trees__, whatever. These would all be incompatible types, because __ints__, __chars__ and __strings__ are incompatible. However, functions I write for __' a tree__s can be used on any type of tree:  
-  
- # __let rec__ count_leaves (t : 'a tree) : int =  
- __match__ t __with__  
- | Leaf _ -> 1  
- | Node (left,right) -> count_leaves left + count_leaves right ;;  
- ''val count_leaves : 'a tree -> int = <fun>''  
-  
- # count_leaves my_tree ;;  
- ''- : int = 3''  
-  
- # count_leaves (Node (Leaf 1, Leaf 2)) ;;  
- ''- : int = 2''  
-  
-Polymorphism is something ML has in common with [Haskell] .  
-  
+PolymorphicTypes are what prevents ML's strong type checking from being a pain in the bum. You don't have to define the type of everything ''exactly'', you can leave some types, or parts of some types unspecified. This allowed you write a great variety of type-safe generic functions
  
 [3] 
 !!!Higher-order Functions