Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] matching on types

Expand Messages
  • Francois Berenger
    ... Here you are (depends on batteries): let string_of_list to_string sep l = [ ^ String.concat sep (BatList.map to_string l) ^ ]
    Message 1 of 10 , Mar 6, 2012
    • 0 Attachment
      On 03/07/2012 02:02 AM, spir wrote:
      > On 03/06/2012 04:28 PM, Dan Bensen wrote:
      > >> 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.

      Here you are (depends on batteries):

      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,
      > Denis
      >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.