99 Problems in OCaml
https://ocaml.org/learn/tutorials/99problems.html
99 problems – OCaml
Your Help is Needed Many of the solutions below have been written by Victor Nicollet. Please contribute more solutions or improve the existing ones. 99 Problems (solved) in OCaml This section is inspired by Ninety-Nine Lisp Problems which in turn was based
ocaml.org
1번 문제
- last
- last list
- list를 인자로 받아서 list의 마지막 원소를 반환한다.
- 반환하는 타입은 option 타입이다.
내가 구현한 코드
빈 list라면 None을 반환
e::l에서 l의 길이가 0이라면 Some e를 반환하고 그렇지 않으면 last l을 재귀 호출한다.
정답 코드
내 코드에서 if문을 쓴 것을 |로 경우를 나눠서 한 것과 같다.
(이렇게도 할 수 있구나!)
test 결과
2번 문제
- last_two
- last_two list
- list를 인자로 받아서 마지막 두 개의 원소를 반환한다.
- 반환하는 타입은 option 타입이다.
내가 구현한 코드 = 정답 코드
빈 리스트거나 리스트 원소가 1개라면 None을 반환하고
리스트 원소가 x, y 2개라면 Some (x, y)를 반환하고 그렇지 않으면 last_two를 재귀호출한다.
test 결과
728x90
'Ocaml' 카테고리의 다른 글
OCaml의 String (0) | 2021.09.11 |
---|---|
[99 Problems in OCaml] 3번, 4번 문제 (0) | 2021.09.10 |
OCaml의 Map (0) | 2021.09.09 |
OCaml의 Loop (0) | 2021.09.06 |
OCaml의 Queue 2 (0) | 2021.09.04 |
댓글