알아야 하는 것
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 = <fun>
# 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 = <fun>
# last_two ["a";"b";"c";"d"];;
- : (string * string) option = Some ("c", "d")
# last_two ["a"];;
- : (string * string) option = None
# let rec at k = function
| [] -> None
| h::t -> if k = 1 then Some h else at (k-1) t;;
val at : int -> 'a list -> 'a option = <fun>
# at 3 ["a";"b";"c";"d";"e"];;
- : string option = Some "c"
# at 3 ["a"];;
- : string option = None
# let length list =
let rec aux n = function
| []->n
| _::t -> aux (n+1) t
in aux 0 list;;
val length : 'a list -> int = <fun>
# length ["a";"b";"c"];;
- : int = 3
# length [];;
- : int = 0
# let rev list =
let rec aux acc = function
| [] -> acc
| h::t -> aux (h::acc) t in
aux [] list;;
val rev : 'a list -> 'a list = <fun>
# rev ["a";"b";"c"];;
- : string list = ["c"; "b"; "a"]
'Computer Science > 프로그래밍언어' 카테고리의 다른 글
TDD( Test Driven Development )이란? (0) | 2022.03.24 |
---|---|
OCaml Tutorial | String 다루기 (0) | 2022.03.18 |
OCaml Tutorial | Recursion with Lists (0) | 2022.03.08 |
OCaml Tutorial | Introduction to Lists (0) | 2022.03.08 |
OCaml Tutorial | Introduction to Pattern Matching (0) | 2022.03.08 |