ocaml tutorial - seoul national universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf ·...
TRANSCRIPT
![Page 1: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/1.jpg)
OCaml Tutorial2011년 봄 프로그래밍 언어 수업
이원찬서울대학교 소프트웨어 무결점 연구센터
![Page 2: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/2.jpg)
들어가기 전에
![Page 3: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/3.jpg)
수업 홈페이지 안내
• 수업 홈페이지
‣ http://ropas.snu.ac.kr/~dreameye/PL
• 조교 홈페이지
‣ http://ropas.snu.ac.kr/~ta/4190.310/11s
![Page 4: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/4.jpg)
두 명의 숙달된 조교들
이원찬 조교
138동 215호에 서식
김진영 조교
302동 312-2호에 계심
![Page 5: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/5.jpg)
설문조사!
• 나는 OCaml을 사용해본 적이 없다.
• 나는 함수형 언어(OCaml, Scheme, Lisp)를 사용해본 적이 없다.
• 나는 프로그래밍을 할 줄 모른다.
• OCaml을 설치는 해봤다.
![Page 6: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/6.jpg)
OCaml은...
• 불행히도 숙제를 위해 새로 배워야 할 언어
• 하지만 더 나은 프로그래밍을 가능케 해주는 언어
• 함수형, 값 중심, 강한 타입 체계를 사용
![Page 7: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/7.jpg)
다운 받는 곳http://caml.inria.fr/download.en.html
![Page 8: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/8.jpg)
설치 방법
• 설치 파일을 사용
‣ Binary distribution란에서 자신의 운영체제용을 받아 설치
• 소스를 받아서 컴파일
‣ Source distribution을 다운받아 압축 해제 후 컴파일
• 패키지 매니저를 사용
‣ apt-get (Ubuntu), port (Mac)에서 ocaml 패키지를 찾을 수 있음!
![Page 9: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/9.jpg)
컴파일 방법
![Page 10: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/10.jpg)
컴파일 방법
![Page 11: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/11.jpg)
실행기 사용 방법
![Page 12: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/12.jpg)
편집은 뭘로?
• 추천하는 편집기
‣ emacs
‣ vi
• 다음으로 추천하는 편집기
‣ Notepad++
‣ Eclipse + OcaIDE
• 뭐든 문법을 잘 드러내는 기능을 지원하는 것으로!
![Page 13: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/13.jpg)
기본기
![Page 14: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/14.jpg)
값 중심의 언어
• 언어 안에서 돌아다니는 모든 것은 값!
let one = 1
let two = 2
let add = (+)
let three = add one two
![Page 15: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/15.jpg)
값 중심의 언어
• 모든 표현식, 값은 타입이 있음
let one = 1 (* int *)
let two = 2 (* int *)
let add = (+) (* int -> int -> int *)
let three = add one two (* int *)
![Page 16: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/16.jpg)
값 중심의 언어
• 모든 표현식, 값은 타입이 있음
let one : int = 1
let two : int = 2
let add : int -> int -> int = (+)
let three : three = add one two
![Page 17: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/17.jpg)
강한 타입 체계
• 열쇠는 꼭 맞는 열쇠 구멍에 끼워야
let one = 1 (* int *)
let two_point_two = 2.2 (* float *)
let add = (+) (* int -> int -> int *)
let three_point_two = add one two_point_two (* error *)
![Page 18: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/18.jpg)
강한 타입 체계
• 열쇠는 꼭 맞는 열쇠 구멍에 끼워야
let one = 1 (* int *)
let two_point_two = 2.2 (* float *)
let add = (+) (* int -> int -> int *)
let three_point_two = (float_of_int one) +. two_point_two (* float *)
![Page 19: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/19.jpg)
함수형 언어
• 함수도 값!
let one = 1 (* int *)
let add = (+) (* int -> int -> int *)
let add_one = add one (* int -> int *)
let four = add_one 3 (* int *)
let five = add_one (add_one 3)
![Page 20: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/20.jpg)
Hello, World그 끝없는 고찰
![Page 21: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/21.jpg)
Hello, World!
let x = print_string “Hello, World!”
실행 결과는... 아시죠?
![Page 22: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/22.jpg)
Hello, World!
let x = print_string “Hello, World!”
print_string: 문자열을 출력하는 “함수”
![Page 23: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/23.jpg)
Hello, World!
let x = print_string “Hello, World!”
print_string의 타입은 string -> unit
![Page 24: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/24.jpg)
Hello, World!
let x = print_string “Hello, World!”
“Hello, World!”: 문자열(string)
![Page 25: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/25.jpg)
Hello, World!
let x = print_string “Hello, World!”
let x = ...: 등호 오른쪽 표현식의 “값”을 앞으로 x로 부름
![Page 26: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/26.jpg)
Hello, World!
let x = print_string “Hello, World!”
print_string “Hello, World!”의 값은?
![Page 27: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/27.jpg)
Hello, World!
let x = print_string “Hello, World!”
print_string “Hello, World!”의 값은? : 빈(unit) 값
![Page 28: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/28.jpg)
Hello, World!
let x = print_string “Hello, World!”
unit타입: 아무 의미 없는 값의 타입
![Page 29: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/29.jpg)
Hello, World! (ver. 2)
let greeting = “Hello, World!”
let _ = print_string greeting
각 라인은 순서대로 실행
![Page 30: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/30.jpg)
Hello, World! (ver. 2)
let greeting = “Hello, World!”
let _ = print_string greeting
let greeting = ...: 저 문자열을 앞으로 greeting으로 부르겠음
![Page 31: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/31.jpg)
Hello, World! (ver. 2)
let greeting = “Hello, World!”
let _ = print_string greeting
greeting은 “이름”이라 값을 바꿀 수 없음!
![Page 32: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/32.jpg)
Hello, World! (ver. 2)
let greeting = “Hello, World!”
let _ = print_string greeting
greeting은 “이름”이라 값을 바꿀 수 없음!, let이외의 장소에서 =는 값이 같은지검사하는 함수
![Page 33: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/33.jpg)
Hello, World! (ver. 2)
let greeting = “Hello, World!”
let _ = print_string greeting
_ : 무관심의 표현
![Page 34: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/34.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
my_print: deco, msg의 두 인자를 받는 함수
![Page 35: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/35.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
e1; e2; ... : 표현식 e1, e2, ... 들을 순서대로 실행
![Page 36: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/36.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
잠깐... 모든 표현식이 값을 갖는 거 아니었나요?
![Page 37: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/37.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
잠깐... 모든 표현식이 값을 갖는 거 아니었나요?: (e1; e2; ...; en)의 값은 en을 실행한 값
![Page 38: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/38.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
따라서 my_print의 몸통을 실행하면
‣ 결과 값은 3, 타입은 int
![Page 39: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/39.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
print_char: 문자 하나를 화면에 출력하는 함수
![Page 40: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/40.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
print_char의 타입은 char -> unit
![Page 41: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/41.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
따라서 my_print의 타입은 char -> string -> int
![Page 42: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/42.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
‘“‘ : 문자 자료형
![Page 43: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/43.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
함수 f를 호출하는 방법: f e1 e2 ...
![Page 44: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/44.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
그럼 f(e1, e2, ...)는 뭔가요?
![Page 45: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/45.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
그럼 f(e1, e2, ...)는 뭔가요?: 주의! 조금 다른 것을 의미합니다!
![Page 46: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/46.jpg)
“Hello, World!” (ver. 3)let greeting = “Hello, World!”
let my_print deco msg = print_char deco; print_string msg; print_char deco; 3
let x = my_print ‘“‘ greeting
실행 결과는?: 화면에 “Hello, World!” 가 출력됨
![Page 47: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/47.jpg)
“Hello, World!” (ver. 3*)let greeting = “Hello, World!”
let my_print = fun deco -> (fun msg -> print_char deco; print_string msg; print_char deco; 3)
let x = my_print ‘“‘ greeting
fun은 도대체 뭔가요?
![Page 48: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/48.jpg)
“Hello, World!” (ver. 3*)let greeting = “Hello, World!”
let my_print = fun deco -> (fun msg -> print_char deco; print_string msg; print_char deco; 3)
let x = my_print ‘“‘ greeting
fun은 도대체 뭔가요?: “함수값”을 만드는 문법
![Page 49: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/49.jpg)
“Hello, World!” (ver. 3*)let greeting = “Hello, World!”
let my_print = fun deco -> (fun msg -> print_char deco; print_string msg; print_char deco; 3)
let x = my_print ‘“‘ greeting
my_print는...‣ deco를 받아서 함수를 돌려줌‣ 그 함수는 msg를 받아서 출력 후 3을 돌려줌
![Page 50: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/50.jpg)
“Hello, World!” (ver. 3*)let greeting = “Hello, World!”
let my_print = fun deco -> (fun msg -> print_char deco; print_string msg; print_char deco; 3)
let x = my_print ‘“‘ greeting
my_print는...‣ 여전히 타입은 char -> string -> unit‣ 같은 뜻으로 char -> (string -> unit)
![Page 51: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/51.jpg)
“Hello, World!” (ver. 4)let greeting = “Hello, World!”
let my_print deco aux msg = aux deco; print_string msg; aux deco
let _ = my_print ‘“‘ (fun c -> print_char c) greeting
my_print: deco, aux, msg의 세 인자를 받는 함수, 하지만...
![Page 52: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/52.jpg)
“Hello, World!” (ver. 4)let greeting = “Hello, World!”
let my_print deco aux msg = aux deco; print_string msg; aux deco
let _ = my_print ‘“‘ (fun c -> print_char c) greeting
aux: deco를 받아들이는 “함수”
![Page 53: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/53.jpg)
“Hello, World!” (ver. 4)let greeting = “Hello, World!”
let my_print deco aux msg = aux deco; print_string msg; aux deco
let _ = my_print ‘“‘ (fun c -> print_char c) greeting
aux: deco를 받아들이는 “함수”함수를 마음대로 함수에 넘겨줄 수 있다!
![Page 54: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/54.jpg)
“Hello, World!” (ver. 4)let greeting = “Hello, World!”
let my_print deco aux msg = aux deco; print_string msg; aux deco
let _ = my_print ‘“‘ (fun c -> print_char c) greeting
my_print에 넘겨줄 “함수값”을 만듭시다!
![Page 55: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/55.jpg)
“Hello, World!” (ver. 4)let greeting = “Hello, World!”
let my_print deco aux msg = aux deco; print_string msg; aux deco
let _ = my_print ‘“‘ (fun c -> print_char c) greeting
my_print에 넘겨줄 “함수값”을 만듭시다!타입은 char -> unit
![Page 56: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/56.jpg)
“Hello, World!” (ver. 4)let greeting = “Hello, World!”
let my_print deco aux msg = aux deco; print_string msg; aux deco
let _ = my_print ‘“‘ (fun c -> print_char c) greeting
my_print의 타입은 따라서,char -> (char -> unit) -> string -> unit
![Page 57: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/57.jpg)
[Hello, World!) (ver. 5)let greeting = “Hello, World!”
let my_print deco msg = match deco with | (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
(‘[‘, ‘)’): ‘[‘, ‘)’ 두 문자를 담은 쌍(pair)을 만듬!
![Page 58: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/58.jpg)
[Hello, World!) (ver. 5)let greeting = “Hello, World!”
let my_print deco msg = match deco with | (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
(‘[‘, ‘)’)의 타입은 char * char
![Page 59: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/59.jpg)
[Hello, World!) (ver. 5)let greeting = “Hello, World!”
let my_print deco msg = match deco with | (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
쌍에 집어 넣은 값은 어떻게 꺼내나요?
![Page 60: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/60.jpg)
[Hello, World!) (ver. 5)let greeting = “Hello, World!”
let my_print deco msg = match deco with | (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
쌍에 집어 넣은 값은 어떻게 꺼내나요?: 패턴 매칭(pattern matching)을 사용!
![Page 61: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/61.jpg)
[Hello, World!) (ver. 5)
“만약 deco가 (_ , _) 모양의 값이라면 쪼개서 prolog, epilog로 이름 붙여 주세요!”
let greeting = “Hello, World!”
let my_print deco msg = match deco with | (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
![Page 62: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/62.jpg)
[Hello, World!) (ver. 5)let greeting = “Hello, World!”
let my_print deco msg = match deco with | (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
그래서 my_print의 타입은char * char -> string -> unit
![Page 63: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/63.jpg)
[Hello, World!) (ver. 5*)
let greeting = “Hello, World!”
let my_print (prolog, epilog) msg = print_char prolog; print_string msg; print_char epilog
let _ = my_print (‘[‘, ‘)’) greeting
ver. 5를 줄여서 쓴 모양설탕 구조 (syntactic sugar)
![Page 64: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/64.jpg)
[Hello, World!) (ver. 6)type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print deco msg = match deco with | Mk_pair (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
type pair = ...: 사용자 정의 타입을 만듬
![Page 65: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/65.jpg)
[Hello, World!) (ver. 6)type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print deco msg = match deco with | Mk_pair (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
Mk_pair: pair 타입의 값을 만드는 생성자
![Page 66: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/66.jpg)
[Hello, World!) (ver. 6)type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print deco msg = match deco with | Mk_pair (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
Mk_pair: pair 타입의 값을 만드는 생성자,반드시 대문자로 시작해야 함!
![Page 67: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/67.jpg)
[Hello, World!) (ver. 6)type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print deco msg = match deco with | Mk_pair (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
pair의 값은 어떻게 쪼개나요?
![Page 68: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/68.jpg)
[Hello, World!) (ver. 6)type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print deco msg = match deco with | Mk_pair (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
pair의 값은 어떻게 쪼개나요?: 패턴 매칭은 여전히 여러분의 친구입니다!
![Page 69: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/69.jpg)
[Hello, World!) (ver. 6)type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print deco msg = match deco with | Mk_pair (prolog, epilog) -> print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
my_print의 타입은 pair -> string -> unit
![Page 70: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/70.jpg)
[Hello, World!) (ver. 6*)
type pair = Mk_pair of char * char
let greeting = “Hello, World!”let my_print (Mk_pair (prolog, epilog)) msg = print_char prolog; print_string msg; print_char epilog
let _ = my_print (Mk_pair (‘[‘, ‘)’)) greeting
ver. 6을 줄여서 쓴 모양
![Page 71: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/71.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
let greeting = ...: 문자의 리스트를 만들어 greeting이라 이름 붙임
![Page 72: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/72.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
greeting의 타입은 char list,string과는 완전 다른 타입!
![Page 73: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/73.jpg)
let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)
a::b: b 리스트 앞에 a가 나타내는 문자를 붙여 “새 리스트”를 만듦
![Page 74: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/74.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
a::b: b 리스트 앞에 a가 나타내는 문자를 붙여 “새 리스트”를 만듦, b값은 변하지 않음!
![Page 75: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/75.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
[] : 빈 리스트를 의미
![Page 76: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/76.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
[‘H’;‘e’;‘l’;‘l’;‘o’]로 줄여서 쓸 수도 있음여전히 설탕 구조 (syntactic sugar)
![Page 77: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/77.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
그럼 리스트는 어떻게 쪼개나요?
![Page 78: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/78.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
그럼 리스트는 어떻게 쪼개나요?: 저희 보람상조... 아니, 패턴매칭이 도와드립니다.
![Page 79: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/79.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
두개의 패턴: 만드는 방법이 두 가지 이므로
![Page 80: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/80.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
“빈 리스트라면”: 손 털고 집에 갑니다
![Page 81: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/81.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
“문자가 한 개 들어있다면”: 해당 문자를 출력하고 나머지 리스트에 대해 반복
![Page 82: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/82.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
rec은 못보던 앤데요?
![Page 83: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/83.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
rec은 못보던 앤데요?: my_print를 정의하는데 my_print를 사용
![Page 84: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/84.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
rec은 못보던 앤데요?: my_print를 정의하는데 my_print를 사용, 재귀함수(recursive function)임을 표시!
![Page 85: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/85.jpg)
반복을 표현하는 두 방법
int a[3] = {1, 2, 3};
for(i = 0; i < 3; ++i) foo(a[i]);
let a = [1; 2; 3]
let rec for x = match x with | [] -> () | b :: x’ -> foo(b); for(x’)
let run = for a
• 절차적 방법(imperative)
• 재귀적 방법(recursive)
![Page 86: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/86.jpg)
반복을 표현하는 두 방법• 절차적 방법
(imperative)• 재귀적 방법
(recursive)
문제 문제
![Page 87: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/87.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 7)let greeting = ‘H’::‘e’::‘l’::‘l’::‘o’::[]let rec my_print msg = match msg with | [] -> () | c :: msg’ -> print_char c; my_print msg’
let _ = my_print greeting
my_print의 타입은 char list -> unit
![Page 88: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/88.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 8)type my_list = Nil | Cons of char * my_list
let greeting = Cons (‘H’, Cons (‘e’, ... , Nil) ... )let rec my_print msg = match msg with | Nil -> () | Cons (c, msg’) -> print_char c; my_print msg’
let _ = my_print greeting
type my_list = ...: 사용자 정의 귀납(inductive) 타입을 만듦
![Page 89: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/89.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 8)type my_list = Nil | Cons of char * my_list
let greeting = Cons (‘H’, Cons (‘e’, ... , Nil) ... )let rec my_print msg = match msg with | Nil -> () | Cons (c, msg’) -> print_char c; my_print msg’
let _ = my_print greeting
값을 만드는 규칙을 또는(|) 기호로 구분해서 적기,생성자 이름은 반드시 대문자로 시작해야함!
![Page 90: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/90.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 8)type my_list = Nil | Cons of char * my_list
let greeting = Cons (‘H’, Cons (‘e’, ... , Nil) ... )let rec my_print msg = match msg with | Nil -> () | Cons (c, msg’) -> print_char c; my_print msg’
let _ = my_print greeting
let greeting = ...: my_list 타입의 값을 만들어서 greeting이라고 부릅시다!
![Page 91: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/91.jpg)
‘H’::‘e’::‘l’::‘l’::‘o’ (ver. 8)type my_list = Nil | Cons of char * my_list
let greeting = Cons (‘H’, Cons (‘e’, ... , Nil) ... )let rec my_print msg = match msg with | Nil -> () | Cons (c, msg’) -> print_char c; my_print msg’
let _ = my_print greeting
값을 쪼갤때는 여전히 패턴 매칭을 사용합니다!
![Page 92: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/92.jpg)
잠깐, 리스트랑 쌍이 뭐가 다른건가요?
(1, 2)(1, (2, 3))(1, (2, (3, 4)))
[1; 2][1; 2; 3][1; 2; 3; 4]
라고 쓴 것,뭐가 다른거죠?
라고 쓴 것과
![Page 93: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/93.jpg)
귀납법의 아름다움(1, 2) (* int*int *)(1, (2, 3)) (* int*(int*int) *)(1, (2, (3, 4))) (* int*(int*(int*int) *)
[1; 2] (* int list *)[1; 2; 3] (* int list *)[1; 2; 3; 4] (* int list *)
이 셋은 모두 같은 타입입니다.
이 셋은 모두 타입이 다릅니다.
![Page 94: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/94.jpg)
귀납법의 아름다움(1, 2) (* int*int *)(1, (2, 3)) (* int*(int*int) *)(1, (2, (3, 4))) (* int*(int*(int*int) *)
[1; 2] (* int list *)[1; 2; 3] (* int list *)[1; 2; 3; 4] (* int list *)
이 셋은 모두 같은 타입입니다.
이 셋은 모두 타입이 다릅니다.
리스트는 길이와 상관없이 같은 타입으로 다룰 수 있습니다!
![Page 95: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/95.jpg)
다 같이 풀어 봅시다
• 숙달된 조교의 시범: length, map
• 여러분이 해 볼것: mem
‣ if문이 필요할 것입니다- if e1 then e2 else e3: e1값이 true면 e2, false면 e3 의 값을 돌려줌 - = : 같은 타입 두 값을 비교해서 true / false를 돌려줌 - then과 else를 항상 써줘야 합니다- then과 else에 오는 표현식은 타입이 같아야 합니다
![Page 96: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/96.jpg)
못다한 이야기
![Page 97: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/97.jpg)
[Hello, World!) (ver. 6**)type pair = { prolog : char; epilog : char }
let greeting = “Hello, World!”let my_print deco msg = print_char deco.prolog; print_string msg; print_char deco.epilog
let _ = my_print { prolog = ‘[‘; epilog = ‘)’} greeting
type pair = ...: 사용자 정의 레코드 타입을 만듬
![Page 98: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/98.jpg)
[Hello, World!) (ver. 6**)type pair = { prolog : char; epilog : char }
let greeting = “Hello, World!”let my_print deco msg = print_char deco.prolog; print_string msg; print_char deco.epilog
let _ = my_print { prolog = ‘[‘; epilog = ‘)’} greeting
record.field: 레코드에서 해당 필드 값을 가져옴
![Page 99: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/99.jpg)
[Hello, World!) (ver. 6**)type pair = { prolog : char; epilog : char }
let greeting = “Hello, World!”let my_print deco msg = print_char deco.prolog; print_string msg; print_char deco.epilog
let _ = my_print { prolog = ‘[‘; epilog = ‘)’} greeting
record.field: 레코드에서 해당 필드 값을 가져옴, 혹은 패턴 매칭으로 쪼갤수도 (각자 찾아보기!)
![Page 100: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/100.jpg)
[Hello, World!) (ver. 6**)type pair = { prolog : char; epilog : char }
let greeting = “Hello, World!”let my_print deco msg = print_char deco.prolog; print_string msg; print_char deco.epilog
let _ = my_print { prolog = ‘[‘; epilog = ‘)’} greeting
{ field = v; ... }: 레코드 타입의 값을 주어진 초기 값을 사용해서 만듦
![Page 101: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/101.jpg)
다형 타입(polymorphic type)
• “리스트는 원소의 내용을 몰라도 만들 수 있다”
1 :: 2 :: []
‘a’ :: ‘b’ :: []
“Hello” :: “World” :: []
![Page 102: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/102.jpg)
다형 타입(polymorphic type)
• “리스트는 원소의 내용을 몰라도 만들 수 있다”
:: :: []
1 :: 2 :: []
‘a’ :: ‘b’ :: []
“Hello” :: “World” :: []
![Page 103: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/103.jpg)
다형 타입(polymorphic type)
• “리스트는 원소의 내용을 몰라도 만들 수 있다”
:: :: []
1 :: 2 :: []
‘a’ :: ‘b’ :: []
“Hello” :: “World” :: []
(* int list *)
(* char list *)
(* string list *)
(* ‘a list *)
![Page 104: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/104.jpg)
다형 타입(polymorphic type)
• 예제type ‘a my_list = | Nil | Cons of ‘a * ‘a my_list
let int_my_list = Cons (1, Cons (2, Nil)) (* int my_list *)
let char_my_list = Cons (‘a’, Cons (‘b’, Nil)) (* char my_list *)
![Page 105: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/105.jpg)
모듈 (module)
• 서로 관련이 있는 코드들의 묶음
module List = (* 이름은 반드시 대문자로 시작 *)struct type ‘a t = Nil | Cons of ‘a * ‘a t
let length : ‘a t -> int = fun l -> ... end
let l = List.Cons (1, List.Nil) (* int List.t *)let length = List.length l (* int *)
![Page 106: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/106.jpg)
모듈 (module)
• 각 파일은 하나의 모듈
type ‘a t = Nil | Cons of ‘a * ‘a t
let length : ‘a t -> int = fun l -> ...
list.ml
![Page 107: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/107.jpg)
모듈 (module)
• 각 파일은 하나의 모듈
type ‘a t = Nil | Cons of ‘a * ‘a t
let length : ‘a t -> int = fun l -> ...
module List = struct
end
![Page 108: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/108.jpg)
모듈 (module)
• 각 파일은 하나의 모듈
type ‘a t = Nil | Cons of ‘a * ‘a t
let length : ‘a t -> int = fun l -> ...
list.ml
let l = List.Cons (1, List.Nil) let length = List.length l
user.ml
![Page 109: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/109.jpg)
모듈 (module)
• 매번 모듈 이름 붙이기 귀찮다면
type ‘a t = Nil | Cons of ‘a * ‘a t
let length : ‘a t -> int = fun l -> ...
list.ml
open Listlet l = Cons (1, Nil)let length = length l
user.ml
![Page 110: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/110.jpg)
아름답지 못한, 허나가끔 유용한
• 참조 타입 (reference)
let a = ref 1 (* int ref *)
let b = !a (* b = 1, int *)
let _ = a := 2 (* unit *)
let c = !a (* c = 2, int *)
![Page 111: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/111.jpg)
아름답지 못한, 허나가끔 유용한
• 배열 (array)
let a = [| 1; 2; 3 |] (* int array *)
let first = a.(0) (* first = 1, int *)
let _ = a.(0) <- 4 (* unit *)
let fisrt‘ = a.(0) (* first‘ = 4, int *)
![Page 112: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/112.jpg)
아름답지 못한, 허나가끔 유용한
• 예외 (exception)
exception Empty_list (* 예외 이름 *) (* 반드시 대문자로 시작 *)
let head l = match l with | [] -> raise Empty_list | a :: l’ -> a
let x = try head [] with Empty_list -> 0 (* x = 0, 예외 처리 *)
![Page 113: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/113.jpg)
좀 더 자세한 이야기는...
• OCaml 사용자 매뉴얼
‣ http://caml.inria.fr/pub/docs/manual-ocaml
• 라이브러리 사용법
‣ http://caml.inria.fr/pub/docs/manual-ocaml/libref/index_modules.html
‣ Pervasives, List, String 등을 참조
• 우리의 영원한 친구 구글
![Page 114: OCaml Tutorial - Seoul National Universityropas.snu.ac.kr/~ta/4190.310/11f/ocaml_tutorial11s.pdf · 2011-08-24 · OCaml Tutorial 2011년 봄 프로그래밍 언어 수업 이원찬](https://reader030.vdocuments.site/reader030/viewer/2022040609/5ecd8593795ce764a30b2010/html5/thumbnails/114.jpg)
끝내며...
• 컴파일 안되는 소스는 절대, Never 채점안합니다.
• 제출 전 최대한 많이 테스트 해봅시다.
• 강조하지만 남의 것을 탐하면 안됩니다.
‣ 우리 연구실은 유사한 코드 찾아내는 기술도 연구하는 곳입니다.
• 조교는 항상 여러분 곁에 있습니다.
‣ 게시판, 메일, 방문의 방법을 적극 활용 바랍니다.