First Asked Questions about writing OCaml - FIAQ

Note:
  # is read-eval-print-loop example - append ";;" to evaluate
  $ is bash/shell command
  keywords of the language appear emphasized within text

What is fun?

 - OCamlers have lot of it.

 < csmrfx> "ocamlers have MORE fun" =
 <@mrvn> type 'a t = Fun of ('a -> 'a);; Fun (fun () -> ());;
 <@mrvn> A unit of fun
 < bitbckt> One (1) International Fun Unit.
 < Qrntz> let rec fun' = (fun () -> ()) :: fun' ;;
 < Qrntz> an endless amount of fun!
 <@mrvn> Qrntz: fun y
 < csmrfx> thats crazy man! 
 <@mrvn> List.iter (fun _ -> print_string "fun\n") fun';;
 < csmrfx> is that just fun or are those funs objects?
 <@mrvn> they aren't objects
 < Qrntz> also, showing haskell fans you can have infinite series in eagerly evaluated languages just for fun!
 < jonafan> yeah...but it only works in special circumstances
 < Qrntz> something like «List.iter (fun fun'' -> fun'' ()) fun'» will work, but, obviously enough, will never 
          terminate
 < Qrntz> so, yes, mostly just for fun



How do I start?

 - Install OCaml for your OS and start OCaml REPL. How to start an OCaml repl? Just go to command line and type "ocaml".



How do I get history in OCaml REPL? - Or - How to get rlwrap with OCaml?

 - Install rlwrap. 

 - Add this alias to your .bashrc:

  alias oc='rlwrap -r -c -D 2 ocaml'

 - Or start OCaml REPL with the command in quotes:

  $ rlwrap -r -c -D 2 ocaml



How to make a function that doesn't take any parameters?

 - Some parameters must be defined. Unit () is a common value to use for "no (more) arguments".

   # fun () -> ()

 - Don't forget to add ";;" and hit enter at the end of the function. The REPL should return:

   - : unit -> unit = <fun>

 - So in OCaml () doesn't denote function call.



What is a parameter?

 - Also known as argument in some programming languages. It is the value given into a function when calling it.



How to define a function "foo" that doesn't take any parameters?

    # let foo () = ()

  - REPL should return:

    al foo : unit -> unit = <fun> 



What is let -clause in OCaml?

  - let binds some expression(s) to a name. For example, the name "foo" to a function definition.



What do the characters ? and ~signify in a let clause ( in OCaml: "let create ?questionmark ~tilde () )?

  ?paramName - argName is an optional parameter.

  ~paramName - the parameter is an labeled parameter.

	example:

	# let askin ?huh = match huh with None -> false | Some x -> x



What is an labeled parameter? 

 - A function argument that is named and can be referred to with that label. Labeled parameters "commute", in other words, change their ordering to match definition.




What is match?

  - An expression that uses patterns to bind identifiers onto parts of the input expression.  For example:

  # let f ?huh = match huh with None -> false | Some x -> x
 
  - It is an powerful expression that is like, but more than the switch/case -statement in other languages.




Whats it mean when an application is total? 

 - Application refers to function application, calling a function.

 - It means that all parameters are given. If only part of the parameters are given, (the function) application is partial. 



What is an partial application?

 - It means using only some of the parameters. Partially applying a function on any parameter create a function of the remaining parameters.



Whats the big deal about partial application?

 < thelema_> csmrfx: a function of type `int -> int -> int` can be thought of as taking two integer parameters
                  and returning an integer
 < thelema_> it can also be thought of as `int -> (int -> int)`, meaning that it takes one integer parameter and
                  returns a function that takes another integer parameter and returns an int
 < thelema_> this is the basis for partial application
 < tchell> csmrfx: see also http://en.wikipedia.org/wiki/Currying
 < Drakken> If a function returns a function, "fully applied" doesn't really say anything meaningful.  
 < csmrfx> I kind of think I know what partial application is, never got what is so big about it
 < Drakken> because you can make functions easily.  like ((+) 1) is an incrementor.



Whoa thats deep! Whats next?

 - See these links:

	The OCaml Basics  http://mirror.ocamlcore.org/ocaml-tutorial.org/the_basics.html
	OCaml core language  http://caml.inria.fr/pub/docs/manual-ocaml/manual003.html