Skip to content

Latest commit

 

History

History
194 lines (141 loc) · 5.44 KB

File metadata and controls

194 lines (141 loc) · 5.44 KB

OCaml

Menu: Home | bash | Compilers | Elixir | F# | Go | Haskell | OCaml | Octave | Perl | Python | R | Rust | Scala | SQL

The Short List

Inbox

F#

F# Differences

Libraries

(* Addition *)

1 + 1
  • Variables
let pi = 3.1416;;

let () = print_endline "Hello, World!"
toplevel

let average a b =
  (a +. b) /. 2.0;;
average;;

let rec // recursion

my_ref := 100;
  • Functions

A function always has exactly one parameter, and returns one result.

(* In this case, the one argument is a tuple, and the one result is a tuple. *)
let swap (x, y) = (y, x);;
let sum (x, y) = x + y;;
sum(3, 5);;
let max (x, y) = if x > y then x else y;;
max(9, 5)

let hi () =
  print_string "hello\n";;
(*
Here, the one argument is the unit, (), which is a value--it is not syntax to indicate an empty parameter list. Similarly, the unit is returned as a result.
*)
print_newline ();;

Include Files

use "myFile.sml"

The most common functions are in the "Pervasives module," which means you don't have to do anything special to use them

open String;; 
length "hello";;

Identifiers must begin with a lowercase letter or underscore, and may contain letters, digits, underscores, and single quotes.

Lists

[2; 3; 5; 7] Only the :: operator (LISP cons) and @ operator (list concatenation) can be used without opening the List module or prefixing the function name with List. .

List Operations

5 :: [6; 7]
[5] @ [6; 7]
List.nth [3; 5; 7] 2
List.rev [1; 2; 3]

Tuples

(5, "hello", ~16)

Tuple Operations

open Char;;
Char.uppercase 'a'

Exceptions

The name of an exception must begin with a capital letter.

You can declare new types of exceptions, with or without a parameter

Comments

(* One line *)
(* 
 * Multi-line
 *)

(* 
 * (* Embedded *)
 *)

opam Package Manager

  • brew install opam