Browse thread
[Caml-list] OCaml 3.05 released
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | John Prevost <j.prevost@c...> |
| Subject: | Re: [Caml-list] OCaml 3.05 released |
>>>>> "sj" == Scott J <jscott@planetinternet.be> writes:
sj> What is meant by first first-class polymorphism?
First class polymorphism in this case lets you define values with
polymorphic types at levels other than the top level, like so:
type foo = { test : 'a 'b . ('a -> 'b) -> 'a list -> 'b list }
let a = { test = List.map }
# a.test;;
- : ('a -> 'b) -> 'a list -> 'b list = <fun>
# a.test ((+) 1) [3; 5; 7];;
- : int list = [4; 6; 8]
# a.test ((^) "a") ["a"; "b"; "c"];;
- : string list = ["aa"; "ab"; "ac"]
For objects, this allows you to define a list type, complete with map
method, as an object. For example:
class ['a] clist x =
object
method map : 'b . ('a -> 'b) -> 'b list = fun f -> List.map f x
end
let a = new clist [1;2;3]
let b = new clist ["a";"b";"c"]
# a #map (fun x -> x + 1);;
- : int list = [2; 3; 4]
# b #map (fun x -> x ^ "-");;
- : string list = ["a-"; "b-"; "c-"]
(Note: this is a very uninteresting definition if a list class, but it
should show you the kind of thing you can now do.)
John.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners