Several Ocaml questions

Ok, this is a longshot but I wil try anyway. I am currently studying Discrete Mathematics which invoves alot of ocaml. Our study material is mostly teacher-made powerpoints without answers and a textbook which only focuses on the math-part. We have now got some training materials that we should try to learn but I am finding it nearly impossible as I can’t find many answers online or in the book.

If someone could try to answer any of the questions I would be really happy:

1: what type does the ML function ( ^) have? when trying in utop I get “bytes -> bytes -> bytes = fun” -does that mean that type is a function? or is it the whole : “bytes -> bytes -> bytes = fun” ?

2: Find two non empty strings s and t so that s ^ t = t ^ s. I would think that that could be only if s and t is the same like “aa” “aa” or “aba” and “aba”

3:What could generally be said about a pair of those commuted strings? I have no idea, that they have to be the same?

4: Let T be the sum of binary strings that does not contains two consecutive ones

4.1 Give a short recursive definition of the set T

4.2 Let An be the number of strings in T of length n. Write a recursive expression for An.

4.3 Write in ML a recursive function gen n with signature val gen: int -> string list that generates a list of such strings. (The strings are represented as common strings with characters ‘0’ and ‘1’.)

5: type operator = Plus | Minus | Times | Id
type value = Zero | One
type term = { op: operator; v: value; aux: value option}

5.1: How many values is there of the type term? I would guess 4*2 but I have no idea how the last value “aux” works.

5.2: Implement the parameterization objecttype 'a option directly as a sum-type in ML

5.3: **Write a ML function : **
let map f = …
val map : (’a ->’b) -> (’a option -> ’b option)
so that: map f None = None and map f (Some x) = Some (f x)

Of #1: the left hand side of the = is the type. The right-hand side is the value. If you enter 42 you’ll see int = 42 in utop’s reply. You see <fun> in the case of a function because functions don’t have meaningful values that can be reported back to you. So yes, the type you’re told is bytes -> bytes -> bytes, which is the type of a function that maps something of type bytes to a function that maps something of type bytes to something of type bytes.

Similarly:

utop # let a = ( ^ ) in let b = a "hi" in b " there";;
- : string = "hi there"
utop # #show_val ( ^ );;
val ( ^ ) : string -> string -> string

Of #5: aux is a value option. Like [One; One; Zero] would be a value list.

utop # #show_type option;;
type nonrec 'a option = None | Some of 'a
utop # type value = Zero | One;;
type value = Zero | One
utop # [None; Some One];;
- : value option list = [None; Some One]

As a general rule, homeworks make for very poor questions (since these questions are often boring outside of their specific learning context) , even moreso when one does not even make any effort to dissimulate the nature of the questions.

From your questions, you need to read an introduction to OCaml (e.g. https://realworldocaml.org, http://caml.inria.fr/pub/docs/manual-ocaml/coreexamples.html ).

As a hint, for you question 3, have you tried s="ab" and t = "abab"?

2 Likes

From the questions, I can infer, that the course assumes a basic knowledge of OCaml. Probably, you are missing some course prerequisites, that makes it so hard for you to understand the course material. Of course, you can solicit for information on online forums (such this one) or Q&A boards (such as stackoverflow), and given that OCaml community is very friendly, you will most likely receive answers. However, it would be hard for you to build the whole picture out of the bits of information that you will gain, so it would be very hard, if not impossible for you to succeed.

Thus my advice for you is to learn OCaml as soon as possible. It’s not hard at all, given that you need only basic OCaml. I would not recommend the Real World OCaml book, as it, as name prescribes, gives you a much more thorough introduction of the language (necessary for the real world development), while you need only the basics. I would personally suggest OCaml from the Very Beginning book. It is not free, however, so if you don’t want to buy for some reasons, then the next two candidates are the Introduction to Objective Caml by Jason Hickey and the OCaml manual itself.

4 Likes