Computer Science/프로그래밍언어 18

OCaml 함수 예제

알아야 하는 것 for문 함수 구조 타입 리스트 인덱싱 match with OCaml version 4.08.1 # let rec last = function | [] -> None | [x] -> Some x | _::t -> last t;; val last : 'a list -> 'a option = # last ["a";"b";"c";"d"];; - : string option = Some "d" # last [];; - : 'a option = None # # let rec last_two = function | [] | [_] -> None | [x;y] -> Some (x,y) | _::t -> last_two t;; val last_two : 'a list -> ('a * 'a) option..

OCaml Tutorial | Introduction to Lists

OCaml version 4.08.1 # [1;2;3];; - : int list = [1; 2; 3] # [];; - : 'a list = [] # [1];; - : int list = [1] # ["hi";"j ^CInterrupted. # ["hi ^CInterrupted. # ["a";"b";"c"];; - : string list = ["a"; "b"; "c"] # [1.2;3.2];; - : float list = [1.2; 3.2] # 선언은 이렇게 # let x : int list = [2;3;4];; val x : int list = [2; 3; 4] # let x = [2;3;4];; val x : int list = [2; 3; 4] let y = [1;2;3];; let is_lis..

OCaml Tutorial | Introduction to Pattern Matching

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 ..