Like the if .. then in code
# let rec range2 a b accum =
if b < a then accum
else range2 a (b - 1) (b :: accum);;
how to write this b < a as a pattern in match .. with ? sth like
let rec range2 a b accum = match .. with
| ..
If you don't need to structurally pattern match anything, then match likely doesn't make sense. If/else exists for reason, after all.
However, you may want to learn about conditional guards on patterns. Let's say I wanted to convert an int to a type with constructors Zero, Even and Odd.
let convert =
function
| 0 -> Zero
| n when n % 2 = 0 -> Even
| _ -> Odd
As compared to:
let convert n =
if n = 0 then Zero
else if n % 2 = 0 then Even
else Odd
Or:
let convert =
function
| 0 -> Zero
| n -> if n % 2 = 0 then Even else Odd
I have seen the following use of pattern-matching to replace if-else (matching on () with guards)
match () with
| _ when b < a -> accum
| _ -> range2 a (b - 1) (b :: accum)
But it is very conflictual whether this is ok to write, or very bad style. Arguments against are that this approach is convoluted / doesn’t use the structual pattern aspect of structural pattern matching. Arguments for are that the match syntax is generally much more readable than the if-else syntax (especially if nesting is involved).
Related
Im trying to run an interpreter I made in ocaml and when i to push in a negative value i.e. let e1 = run [PushI -2; PushI 2; LessThan] []. I am getting a syntax error for my parse_int function. I'm trying to write the part of the function that allows for the input of a negative number
type stackVal =
I of int
type command = PushI of int
let rec run (commands : command list) (stack: stackVal list) : stackVal list =
match (commands , stack) with
| (PushI i :: rest, _ ) -> run rest (I i :: stack)
let to_string (s : stackVal) : string =
match s with
| I i -> string_of_int i
let parse_command (s:string) : command =
match take_while is_alpha (String.trim s) with
| ("PushI" , p) -> let Some i = parse_int (String.trim p) in PushI i
let parse_int (s : string) : int option =
match int_of_string s with
| String.get n 0 = '-' -> Some -String.sub n 1 len
| n -> Some n
| exception _ -> None
There is a problem with the pattern-matching of your parse_int function.
match int_of_string s with
| String.get n 0 = '-' -> Some -String.sub n 1 len
| n -> Some n
| exception _ -> None
The first clause here is invalid as "String.get n 0 = '-'" is not an integer constructor. You could write 1 which matches only the integer 1 or _ whitch matches any integer or n which matches any integer and binds it to the name n for the rest of the clause. You can have a look at the manual for more informations.
If you wanted to check if the first char of the string is - pattern matching is not the right tool to do it, simply use an if then else.
However, note that int_of_string works just fine on negative integers, so there is no need to do that part by yourself.
Unrelated, but i noticed that you call the parse_int in the parse_command function. In that case you should define parse_int before parse_command.
I'm trying to figure out how to pattern match with user defined types. For example I have this type.
Type custom_type = B of bool | I of int | S of string | C of custom_type * custom_type
I want to pattern match these types, and say for example count the number of ints in a value. Example value:
C(C(B true, I 5), C(S "example", B false))
I think I'm very close to figuring it out, I know I need to use wildcards but I can't write out every instance there could be, because there are numerous varying values I need to check.
Thanks!
Edit: Code that isn't working:
let num = 0
let rec count_ints (c: custom_type):int =
match c with
| C (I(_), _) -> num + 1
| C (_, I(_)) -> num + 1
| C (C(_), _) -> count_ints c
| C (_, C(_)) -> count_ints c
You should be thinking of having 4 cases in your function, one for each constructor. You don't need to match what's inside these constructors because you can call yourself recursively to handle that.
Your code calls count_chars, but there's no function of that name. If it's supposed to be count_ints, then this is not a good recursive call. You must call recursively on a smaller problem. If you just pass c along to yourself recursively you'll get infinite recursion.
let rec count_ints (c: custom_type):int =
match c with
| I _ -> 1
| C (c1,c2) -> count_ints c1 + count_ints c2
| _ -> 0
let rec parity n = if (n = 0) then
print_string "even" else if (n = 1)
print_string "odd" else
parity (n-2);;
In comparison to...
let rec parity n =
match n with
| 0 -> "even"
| 1 -> "odd"
| _ -> parity(n-2);;
I'm still new to this language. The error in the if-then-else is specifically on my print_string statements w/ a syntax error.
Your second if is missing its then.
(The expression after if doesn't need to be parenthesized in OCaml, incidentally.)
I want to build a list of type (char, 'a list) list where each char is an upper case letter of the alphabet. I'am getting a warning Warning 11: this match case is unused. for the second match case on get_list. I did some prints on the first case and found out len get's there with value 0, so it never uses the second case. What's happening?
let rec get_list abc i len =
match i with
| len -> []
| _ -> ((String.get abc i), [])::get_list abc (i + 1) len
in
let rec print_list l =
match l with
| [] -> ()
| h::t -> print_char(fst h);print_list t
in
let abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in
let abc_l = get_list abc 0 (String.length abc) in
print_list abc_l;;
The reason it doesn't work
When you write
match i with
| len -> []
| _ -> ["..."]
len is a generic pattern, which hasn't anything to do with the len define above. In a pattern matching you define only how the variable should look like, you describe it's general "structure", the variable names are used to name the differents parts of the pattern matching, and are new variables. For example with lists you can do:
match my_list with
| [x,y,z] -> x+y+z
| x :: r -> x + (List.length r)
| anything_else -> List.length anything_else
When you put '_' it's only a convention to say "I don't mind which value it is, I don't need it". Here is another example with tuples:
match my_tuple with
| (a,b) -> a+b
A solution : conditionnal pattern matching
If you want to put condition in a pattern matching you can use the when keyword :
match i with
| n when n = len -> []
| _ -> ["..."]
Another example that "sort" a tuple:
match my_tuple with
| (a,b) when a>b -> (a,b)
| (a,b) -> (b,a)
Or just use conditions with integers :
if i = len then []
else ["..."]
You can also note that you can do pattern matching within functions :
let f (a,b) = a+b
The len in your pattern is a new variable introduced by the pattern. As a pattern, its meaning is that it will match anything at all. Thus, the next pattern _ will never match.
As #AlexanderRevyakin says, this new variable len is hiding the parameter that's also named len.
It is not the case that the len in your pattern represents the value of the parameter len. OCaml patterns contain only new variables (to which pieces of the matched value are bound) and constants. They don't contain expressions that are evaluated at run time. For that you want to use if/then/else (as #AntonTrunov points out).
I'm writing a code that can find the median of a list, and I cannot use rec and should use List.fold_left/right. I wrote the following code, which should work.
It finds the length of the list, if it's an odd number like 5, then it sets len1, len2 to 2, 3, if it's an even number like 6, then it sets len1, len2 to 2, 3.
Then for each member in the list I match the number of those elements that are less than it.
However, the following pattern matching always math lessNum elmt to len1 - can someone tell me why it is so?
let median (lst : int list) : float option =
let len = List.length lst in
if lst = [] then None
else
let len1, len2 = (len - 1) / 2, (len + 1) / 2 in
let lessNum a =
List.length (List.find_all (fun n -> n < a) lst) in
let answer = List.fold_left (fun accm elmt ->
match (lessNum elmt) with
| len1 -> accm + elmt
| len2 -> failwith "len2"
| _ -> failwith "other"
) 0 lst in
if len mod 2 = 0
then Some ((float_of_int answer) /. 2.0)
else Some (float_of_int answer)
An identifier appearing in a pattern always matches, and binds the corresponding value to the identifier. Any current value of the identifier doesn't matter at all: the pattern causes a new binding, i.e., it gives a new value to the identifier (just inside the match).
# let a = 3;;
val a : int = 3
# match 5 with a -> a;;
- : int = 5
# a;;
- : int = 3
#
So, your match statement isn't doing what you think it is. You'll probably have to use an if for that part of your code.
Update
Here's how to use an association list to approximate the function f in your followup question:
let f x = List.assoc x [(pat1, ans1); (pat2, ans2)]
This will raise a Not_found exception if x is not equal to pat1 or pat2.
(I think your Python code is missing return.)