Re: "ocaml_beginners":: matching on types
- On 03/07/2012 02:02 AM, spir wrote:
> On 03/06/2012 04:28 PM, Dan Bensen wrote:Here you are (depends on batteries):
> >> What method would you employ to write a generic print func
> > That's difficult. You have to change the compiler or maybe use camlp4.
> > There's a project in progress somewhere, but I don't have a link.
> > Batteries has print functions for containers, so you can at least
> > write your own print and to_string functions in a modular way.
> > Here's a list-printing function if you don't want to use Batteries:
> > let print_list print_x ?(first="[") ?(sep=";") ?(last="]") xs =
> > print_string first;
> > let rec aux = function
> > |  -> print_string last
> > | [x] -> print_x x; print_string last
> > | x::xs -> print_x x; print_string sep; aux xs
> > in aux xs
> All right. I think this is the approach I was looking for: passing a
> type-specific element-printing func to a generic list-printing
> higher-order func. I will follow this this track.
let string_of_list to_string sep l =
"[" ^ String.concat sep (BatList.map to_string l) ^ "]"
> I would use Batteries, but for now I prefere to take the problems met on
> my way as opportunities to "play" with OCaml and discover it better.
> Thank you, Dan,