OCaml · Lesson 1 · On the house

Hello OCaml

print_endline, let () =, comments, utop, and ocaml or dune to run. Lesson 1 is free — on the house.

Lessons · OCaml · Lesson 1 of 10 · On the house

Hello OCaml

print_endline, let () =, comments, utop, and ocaml or dune to run.

Illustration for OCaml
Lesson 1 is on the house.

print_endline "hello" prints a string and a newline. Function application is just a space: no parentheses around arguments, no commas.

A program's top-level effects are usually written let () = print_endline "hello". The () pattern says the expression returns unit, OCaml's 'no meaningful value'.

Comments are (* like this *) and they nest. There are no line comments.

Try code in utop, the interactive toplevel, ending phrases with ;;. Run a file with ocaml hello.ml, or build projects with dune build and dune exec ./hello.exe.

Misses: writing print_endline("a", "b") with a comma (that is a tuple), and using // comments.

hello

(* hello.ml *)
let () =
  print_endline "hello";
  print_endline (string_of_int (2 + 3))
Match this when you type.

Quiz

How do you call print_endline with "hi"?

Quiz

What does let () = ... express?

Quiz

How is a comment written?

Quiz

Which tool is the interactive OCaml toplevel?

Check

Match the still for: Hello OCaml.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.