Tailrecursive Function Highorder Function 20090403 Tailrecursive Function 1

![Tail-recursive Function • [문제 1] factorial 을 계산하는 함수 fact 를 recursive function 으로 Tail-recursive Function • [문제 1] factorial 을 계산하는 함수 fact 를 recursive function 으로](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-2.jpg)
![Tail-recursive Function • [모범답안] let rec fact n = if n = 0 then Tail-recursive Function • [모범답안] let rec fact n = if n = 0 then](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-3.jpg)
![Tail-recursive Function • [실습] factorial 을 계산하는 함수 fact 를 tail-recursive function 으로 작성한다. Tail-recursive Function • [실습] factorial 을 계산하는 함수 fact 를 tail-recursive function 으로 작성한다.](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-4.jpg)

![Tail-recursive Function • [모범답안] let rec pow a b = if b = 0 Tail-recursive Function • [모범답안] let rec pow a b = if b = 0](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-6.jpg)
![Tail-recursive Function • [실습] exponentiation 을 계산하는 함수 pow 를 tail-recursive function 으로 작성 Tail-recursive Function • [실습] exponentiation 을 계산하는 함수 pow 를 tail-recursive function 으로 작성](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-7.jpg)
![Tail-recursive Function • [문제 3] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 recursive Tail-recursive Function • [문제 3] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 recursive](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-8.jpg)
![Tail-recursive Function • [모범답안] let rec bin 2 dec if x = 0 then Tail-recursive Function • [모범답안] let rec bin 2 dec if x = 0 then](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-9.jpg)
![Tail-recursive Function • [실습] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 tail-recursive function Tail-recursive Function • [실습] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 tail-recursive function](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-10.jpg)
![Tail-recursive Function • [오답] 착각하기 쉬운 답 (하지만 반대로 계산 됨 -. -) let Tail-recursive Function • [오답] 착각하기 쉬운 답 (하지만 반대로 계산 됨 -. -) let](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-11.jpg)
![Tail-recursive Function • [모범답안] 또 하나의 변수를 이용해야 함 let bin 2 dec n Tail-recursive Function • [모범답안] 또 하나의 변수를 이용해야 함 let bin 2 dec n](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-12.jpg)



![High-order Function • [실습] string_adder s 는 어떤 문자열 앞 에 s를 삽입하여 반환하는 High-order Function • [실습] string_adder s 는 어떤 문자열 앞 에 s를 삽입하여 반환하는](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-16.jpg)
![High-order Function • [모범답안]두 가지 방법 let string_adder s = fun x -> s High-order Function • [모범답안]두 가지 방법 let string_adder s = fun x -> s](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-17.jpg)

![High-order Function • [모범답안] let integral f a b = let rec integral' f High-order Function • [모범답안] let integral f a b = let rec integral' f](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-19.jpg)
- Slides: 19

Tail-recursive Function, High-order Function 전자계산입문 2009/04/03
![Tailrecursive Function 문제 1 factorial 을 계산하는 함수 fact 를 recursive function 으로 Tail-recursive Function • [문제 1] factorial 을 계산하는 함수 fact 를 recursive function 으로](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-2.jpg)
Tail-recursive Function • [문제 1] factorial 을 계산하는 함수 fact 를 recursive function 으로 작성한다. • [실행결과]
![Tailrecursive Function 모범답안 let rec fact n if n 0 then Tail-recursive Function • [모범답안] let rec fact n = if n = 0 then](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-3.jpg)
Tail-recursive Function • [모범답안] let rec fact n = if n = 0 then 1 else n * fact (n - 1) ; ;
![Tailrecursive Function 실습 factorial 을 계산하는 함수 fact 를 tailrecursive function 으로 작성한다 Tail-recursive Function • [실습] factorial 을 계산하는 함수 fact 를 tail-recursive function 으로 작성한다.](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-4.jpg)
Tail-recursive Function • [실습] factorial 을 계산하는 함수 fact 를 tail-recursive function 으로 작성한다. • [모범답안] let fact n = let rec fact' n accum = if n = 0 then accum else fact' (n - 1) (n * accum) in fact' n 1 ; ;

![Tailrecursive Function 모범답안 let rec pow a b if b 0 Tail-recursive Function • [모범답안] let rec pow a b = if b = 0](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-6.jpg)
Tail-recursive Function • [모범답안] let rec pow a b = if b = 0 then 1 else a * pow a (b - 1) ; ;
![Tailrecursive Function 실습 exponentiation 을 계산하는 함수 pow 를 tailrecursive function 으로 작성 Tail-recursive Function • [실습] exponentiation 을 계산하는 함수 pow 를 tail-recursive function 으로 작성](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-7.jpg)
Tail-recursive Function • [실습] exponentiation 을 계산하는 함수 pow 를 tail-recursive function 으로 작성 한다. • [모범답안] let pow a b = let rec pow' a b s = if b = 0 then s else pow' a (b-1) (s*a) in pow' a b 1 ; ;
![Tailrecursive Function 문제 3 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 recursive Tail-recursive Function • [문제 3] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 recursive](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-8.jpg)
Tail-recursive Function • [문제 3] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 recursive function 으로 작성 한다. • [실행결과]
![Tailrecursive Function 모범답안 let rec bin 2 dec if x 0 then Tail-recursive Function • [모범답안] let rec bin 2 dec if x = 0 then](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-9.jpg)
Tail-recursive Function • [모범답안] let rec bin 2 dec if x = 0 then else if x = 1 else 2 * ; ; x = 0 then 1 bin 2 dec (x/10) + x mod 10
![Tailrecursive Function 실습 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 tailrecursive function Tail-recursive Function • [실습] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 tail-recursive function](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-10.jpg)
Tail-recursive Function • [실습] 2진수를 10진수로 바꾸는 함수 bin 2 dec 을 tail-recursive function 으로 작성한다. • [실행결과]
![Tailrecursive Function 오답 착각하기 쉬운 답 하지만 반대로 계산 됨 let Tail-recursive Function • [오답] 착각하기 쉬운 답 (하지만 반대로 계산 됨 -. -) let](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-11.jpg)
Tail-recursive Function • [오답] 착각하기 쉬운 답 (하지만 반대로 계산 됨 -. -) let bin 2 dec n = let rec bin 2 dec' n accum = if n = 0 then accum else bin 2 dec' (n / 10) (accum * 2 + n mod 10) in bin 2 dec' n 0 ; ;
![Tailrecursive Function 모범답안 또 하나의 변수를 이용해야 함 let bin 2 dec n Tail-recursive Function • [모범답안] 또 하나의 변수를 이용해야 함 let bin 2 dec n](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-12.jpg)
Tail-recursive Function • [모범답안] 또 하나의 변수를 이용해야 함 let bin 2 dec n = let rec bin 2 dec' n m accum = if n = 0 then accum else bin 2 dec' (n / 10) (m * 2) (accum + m * (n mod 10)) in bin 2 dec' n 1 0 ; ;



![Highorder Function 실습 stringadder s 는 어떤 문자열 앞 에 s를 삽입하여 반환하는 High-order Function • [실습] string_adder s 는 어떤 문자열 앞 에 s를 삽입하여 반환하는](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-16.jpg)
High-order Function • [실습] string_adder s 는 어떤 문자열 앞 에 s를 삽입하여 반환하는 함수 작성 • [실행결과] # string_adder; ; - : string -> string = <fun> # let hello_adder = string_adder "Hello "; ; val hello_adder : string -> string = <fun> # hello_adder "della"; ; - : string = "Hello della" # let bye_adder = string_adder "Bye "; ; val bye_adder : string -> string = <fun> # bye_adder "della"; ; - : string = "Bye della"
![Highorder Function 모범답안두 가지 방법 let stringadder s fun x s High-order Function • [모범답안]두 가지 방법 let string_adder s = fun x -> s](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-17.jpg)
High-order Function • [모범답안]두 가지 방법 let string_adder s = fun x -> s ^ x; ; let string_adder s = let h s x = s ^ x in h s ; ;

![Highorder Function 모범답안 let integral f a b let rec integral f High-order Function • [모범답안] let integral f a b = let rec integral' f](https://slidetodoc.com/presentation_image_h/d533f96bcb921defaa6e88d645007c08/image-19.jpg)
High-order Function • [모범답안] let integral f a b = let rec integral' f a b s = if a = b then s else integral' f (a+1) b (f a + s) in integral' f a b 0 ; ;