4

I have an Ocaml function that is giving me errors.

What I am trying to do:

Recursively create a List of random numbers (0-2) of size "limit".

Here's what I have:

let rec carDoorNumbers = fun limit -> 
match limit with
| [1] -> Random.int 3
| [] -> Random.int 3 :: carDoorNumbers (limit-1);;

I am getting this error:

Error: This expression has type 'a list
   but an expression was expected of type int

2 Answers 2

3

Think about what your function has to do: given a limit, you have to create a list of numbers. So your type is something like carDoorNumbers : int -> int list.

Looking at that, it seems you have two errors. First, you're matching limit (which should be an int) against a list pattern. [1] -> ... matches a list containing only the element 1 and [] matches the empty list; you really want to match against the number 1 and any other number n.

The second error is that you return two different types in your match statement. Remember that you are supposed to be returning a list. In the first case, you are returning Random.int 3, which is an int rather than an int list. What you really want to return here is something like [Random.int 3].

The error you got is a little confusing. Since the first thing you returned was an int, it expects your second thing to also be an int. However, your second case was actually correct: you do return an int list! However, the compiler does not know what you meant, so its error is backwards; rather than changing the int list to an int, you need to change the int to an int list.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay that makes sense. For my second case, instead of comparing to an empty list [], how would I just use it for all other possibilities for limit (I assume its a pos. #)?
The argument is a number. You want to match any number (except 1, but that gets matched by the previous case). You can do this with the simplest pattern n, which matches anything and binds it to the variable n. (You can naturally name the variable whatever you want.)
2

Your match expression treats limit like a list. Both [1] and [] are lists. That's what the compiler is telling you. But it seems limit should be an integer.

To match an integer, just use an integer constant. No square brackets.

(As a side comment, you might want to be sure the function works well when you pass it 0.)

2 Comments

Yea I definitely am not accounting for all inputs, assuming pos. integers. Thanks, I'll take out the brackets. How do I make the second case run for every possible "limit" except 1? The more I think about it the more I think I need if/else statements.
The "wild card" pattern is _, which will work for this code. There's no harm at all in using if/then/else in OCaml. That's what I usually use for ints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.