Presentation is loading. Please wait.

Presentation is loading. Please wait.

Indent Style, Recursive Function 전자계산입문 2009/03/27.

Similar presentations


Presentation on theme: "Indent Style, Recursive Function 전자계산입문 2009/03/27."— Presentation transcript:

1 Indent Style, Recursive Function 전자계산입문 2009/03/27

2 Indent Style 숙제를 도와 달라는 어떤 학생 … let rec collatz n = if n = 1 then true else if n mod 2 = 1 then collatz (3*n + 1) else collatz (n/2);; 니가 해 !!!!

3 Indent Style 간단한 경우에는 한 줄에 깔끔하게 작성 let incr x = x + 1;; let concat s = “Hello” ^ s;;

4 Indent Style if 문의 경우 if ~ then ~ 이 길어지는 경우 if x > 0 then “positive” else if x < 0 then “negative” else “zero” if x > 0 && y > 0 && z > 0 then x + y > z && y + z > x && z + x > y else false

5 Indent Style 함수가 들어간 보다 복잡한 경우 let rec collatz n = let a = 3 in let f x = if x > 0 then “positive” else “non-positive” in if n = 1 then true else if n mod 2 = 1 then collatz (3*n + 1) else collatz (n/2) ;;

6 Tuple 불필요한 pattern matching 을 피함 let add a b = let (a1, a2) = a in let (b1, b2) = b in (a1 + b1, a2 + b2) ;; let add (a1, a2) (b1, b2) = (a1 + b1, a2 + b2) ;;

7 Misc. Indentation 을 줄 때, tab 을 쓰는 것 보다공 백 ( 스페이스바 ) 을 사용하는 것이 좋음. (2 칸 정도가 적당, 4 칸을 넘지 말 것 ) 불필요한 조건문을 쓰지 말 것. if x = 0 then true else false x = 0

8 Misc. 수식은 보기 좋게 적당히 띄어줌. 11*(19mod3)+17/4-6;;

9 Recursive Function [ 문제 ] 문자열 s 를 입력받아 s 가 두번 반복 된 문자열을 반환하는 함수 string2 작성하 라. [ 실행결과 ] [ 모범답안 ] let string2 s = s ^ s;;

10 Recursive Function [ 문제 ] 문자열 s 와 정수 n 을 입력받아 s 가 n 번 반복된 문자열을 반환하는 함수 stringn 을 작성하라. … 1 번 : let string1 s = s ;; 2 번 반복 : let string2 s = s ^ s ;; 3 번 반복 : let string3 s = s ^ s ^ s ;; n 번 반복 : let stringn s n = ??

11 Recursive Function … [ 수행 ] n : s ^ stringn s (n-1) n-1 : s ^ s ^ stringn s (n-2) n-2 : s ^ s ^ s ^ stringn s (n-3) 2 : s ^ s ^ s ^ … ^ s ^ stringn s (2-1) 1 : s ^ s ^ s ^ … ^ s ^ s 1.termination condition: 2.base case: 3.recursive case: if then else n = 1 s s ^ stringn s (n – 1)

12 Recursive Function [ 모범답안 ] let rec stringn s n = if n = 1 then s else s ^ stringn s (n - 1) ;;

13 Recursive Function 2 진수를 10 진수로 바꾸는 문제 x = 0 이면 0 x = 1 이면 1 Base case: 한자리만 남았을 경우 Recursive case: 함수가 제대로 기능한다고 치고 만듬 한 자리씩 한 자리씩 recursion 하면서 계산하자 ! 101101 ( 앞의 숫자들로 계산한 값 ) x 2 + 마지막 숫자 if x = 0 then 0 else if x = 1 then 1 else bin2dec (x/10) * 2 + x mod 10

14 Recursive Function [ 모범답안 ] let rec bin2dec x = if x = 0 then 0 else if x = 1 then 1 else 2 * bin2dec (x/10) + x mod 10 ;;

15 [ 연습문제 1] [ 문제 ] factorial 을 계산하는 함수 fact 를 recursive function 으로 작성한다. [ 실행결과 ]

16 [ 연습문제 1] [ 모범 답안 ] let rec fact n = if n = 0 then 1 else n * fact (n - 1) ;;

17 [ 연습문제 2] [ 문제 ] exponentiation 을 계산하는 함수 pow 를 recursive function 으로 작성한다. ( 인자 a 와 b 를 받아 a b 를 계산하는 함수 pow 작성 ) [ 실행결과 ] Pow 3 20(=3 20 ) 과 같이 pow 함 수를 적용하는 경우 Ocaml 이 표현 할 수 있는 정수의 범위를 벗어나 overflow 가 발생한다.

18 [ 연습문제 2] [ 모범답안 ] let rec pow a b = if b = 0 then 1 else a * pow a (b - 1) ;;


Download ppt "Indent Style, Recursive Function 전자계산입문 2009/03/27."

Similar presentations


Ads by Google