Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

OCaml

Name: Anonymous 2011-07-22 0:24

What do you think of OCaml?

Right now it's my favorite language. It's actually practical and fast (unlike Haskell). Also I can use imperative style whenever I feel like it but I still have all of the goodness from using a functional language. I am starting to use it as my regular language.

Name: Anonymous 2011-07-24 13:53

>>11
Ocaml (and SML) has an excellent module system and it has functors. Functors are basically functions that map modules to modules.

So let's say you want to create a Set data structure for any type. Elements of the data structure would have to be ordered. Ordering can be done using a compare function for that particular type. So first you would create a module for a certain type (say int) with a signature:

module type OrderedType = sig
type t
val compare : t -> t -> int
end


and then you can create the Set functor which reads in a module like


module OrdInt : OrderedType = struct
type t = int
let compare a b = if a < b ... blah bla


of type OrderedType and outputs a Set module that have elements of type int.

The Set functor would look something like


module Set = functor (Ord : OrderedType) -> struct

type elt = Ord.t
type set = [elt]

let empty a = (a = [])
let member xs x = ... blah blah

end


and you can then create sets of ints like


module IntSet = Set(OrdInt)



So in the end you can create your own data structure d and create d sets. Then use the Set datastructure without having to pass the compare function all the time.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List