Recitation 2

This recitation is a bit longer than most will be, because we need to hit the ground running. The questions below are designed to guide your initial explorations of OCaml. Use sections 2.2 through 2.5 of the textbook, the manual, and the other students in your recitation section to help you!

  1. How do OCaml if expressions compare and contrast with if commands in imperative languages, such as Java and Python? And with ternary operators? (If you’re not already familiar with ternary operators in those languages, now’s a good time to look them up!)

  2. Choose one or two simple mathematical functions and implement them in OCaml. If you need inspiration, perhaps think of some simple equations from a physics or algebra or statistics class. All the built-in mathematical functions (e.g., sin, log, etc.) are described in the documentation of the Pervasives module, in case you need them.

  3. In your own words, explain the difference between syntax and semantics. Also explain the difference between static semantics and dynamic semantics.

  4. When using the OCaml build system, what files get created, and where are they are located? Experiment as necessary to find out.

  5. The section on scope in the textbook discusses this expression:
    let x = 5 in 
      ((let x = 6 in x) + x)
    

    Using the dynamic semanics of let expressions, explain how that expression evaluates.

  6. Run the following code in the toplevel.
    let x = 1;;
    let x = 2;;
    x;;
    

    Why is it valid to say that variables in OCaml are immutable, despite the output of utop? Your answer should incorporate the ideas of shadowing and nested let expressions.

Submit to CMS: Submit your answers to the questions above. Type your answers. Please do not submit handwritten answers. Keep it brief—definitely no more than one page. For the last question, just include your code as part of your written answers. There’s no need to submit a .ml file.