News for hackers. Stuff that matters.
Re:Why OCaml, why now? (2014) by yodsanklai on Saturday February 14, 2015 @05:13PM (#9050949)
One thing I don't like about OCaml is that I always find myself writing the same things, like "to_string" functions for my variant types (although there must be some ways to alleviate this burden).

Also, when your programs use abstract data types you lose the benefits of pattern matching. In that case, I'm happier with languages like Go or Ada with a friendlier syntax.

Re:Why OCaml, why now? (2014) by tel on Saturday February 14, 2015 @06:04PM (#9051062)
If you have an ADT but want pattern matching, try Scott Encoding.

For instance, here we have Option

    module Option = struct
      let scott (some : 'a -> 'r) 
                (none : 'r) 
                (opt  : 'a option) =
        match opt with
        | Some a -> some a
        | None   -> none
    end
For Option, since it's non-recursive, the Scott Encoding and the recursor/inductor/Church Encoding are identical. Here's a linked list, though

    module LL : sig
      type 'a t
      val fold  : ('a -> 'r -> 'r) -> 'r -> ('a t -> 'r)
      val scott : ('a -> 'a t -> 'r) -> 'r -> ('a t -> 'r)
    end = struct      
      type 'a t = Cons of 'a * 'a t | Nil
      let rec fold cons nil = function
        | Cons (h, t) -> cons h (fold cons nil t)
        | Nil         -> nil
      let scott cons nil = function
        | Cons (h, t) -> cons h t
        | Nil         -> nil
    end
Anyway, the pattern should be more clear now. These provide effectively "functionalized" pattern matching which you can apply whenever you need. In particular, you can think of these as expressing a (potentially partial) "view" of the abstract type. For instance, my linked list might have not been a linked list exactly but instead some kind of tree, but `scott` and `fold` let me expose a "view" of that tree as though it were a linked list.
Re:Why OCaml, why now? (2014) by ufo on Saturday February 14, 2015 @06:44PM (#9051165)
If you look at this from an "expression problem" point of view, this version with the records-of-functions is very similar to OO programming. But without inheritance, classes and so on.
Re:Why OCaml, why now? (2014) by tel on Saturday February 14, 2015 @07:57PM (#9051385)
I'm very much a fan of that POV. I think OO has a lot of mythology, but the technology is relatively similar to something like OCaml or Haskell. Classes are essentially just functions which return "structures".

Inheritance and open-recursion/dynamic binding are usually what remain. I'm not terribly sure I miss them.

Re:Why OCaml, why now? (2014) by lpw25 on Saturday February 14, 2015 @08:05PM (#9051404)
Indeed classes are great for these kinds of encodings. It is one of the use cases for which classes are the nicest approach in OCaml.

For example, see this section of Real World OCaml:

https://realworldocaml.org/v1/en/html/classes.html#open-recu...

Re:Why OCaml, why now? (2014) by emillon on Sunday February 15, 2015 @07:00AM (#9052400)
> Also, when your programs use abstract data types you lose the benefits of pattern matching.

In some cases you can also use "private ADTs". Modules seeing such a type can use pattern matching on them but can not apply the constructors.

Re:Why OCaml, why now? (2014) by mercurial on Sunday February 15, 2015 @03:00AM (#9052054)
PPX lets you generate show functions automatically. As for abstract types you can use "private" if you need to pattern match outside the module but wont to ensure that the structure can only be created by your module.