Computer Science/프로그래밍언어

OCaml Tutorial | Introduction to Pattern Matching

토마토. 2022. 3. 8. 18:26

let is_zero (x:int) : string =
        match x with
                | 0 -> "true"
                | 1 -> "false";;
print_string (is_zero 0);;

 

c언어의 case랑 비슷한 기능인 것 같음

@martini:~$ ocaml pattern.ml
File "./pattern.ml", line 2, characters 8-81:
2 | ........match x with
3 |                 | 0 -> "true"
4 |                 | 1 -> "false"..
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
2
true

 

이 에러 해결하려면, 

 

let is_zero (x:int) : string =
        match x with
                | 0 -> "true"
                | _ -> "false";;
print_string (is_zero 0);;

이렇게 수정하기