CSED101 INTRODUCTION TO COMPUTING RECURSIVE FUNCTION ( 자기호출함수 )

  • Upload
    salaam

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

유환조 Hwanjo Yu. CSED101 INTRODUCTION TO COMPUTING RECURSIVE FUNCTION ( 자기호출함수 ). Review: Local variable. let = in let = in … let = in Try in OCAML let x = 1+1 in let y = 2 in x + y;;. - PowerPoint PPT Presentation

Citation preview

  • CSED101 INTRODUCTION TO COMPUTINGRECURSIVE FUNCTION ()Hwanjo Yu

  • Review: Local variablelet = inlet = inlet = in

    Try in OCAMLlet x = 1+1 inlet y = 2 inx + y;;*

  • Review: Local variablelet = let = in in ;;What would be the value of x in let x = let y = 1 + 1 in y + y in x + x ?Too complicated? Lets rewrite it intolet x =let y =1+1in y+yin x+x?=> 8*

  • Review: Functionlet = fun .. -> ;;let .. = ;;let f = fun a ->let g = fun x -> a*xin (g 1) + (g 2);;let f a =let g x =a * xin (g 1) + (g 2);;f 1;;f 2;;

    *

  • Review: TupleTuple to return multiple values in a functionlet h x y = (x+y, x*y);;Tuple as function argumentslet f (x, y) (v, w) = x*v + y*w;;let f a b =let (x, y) = a inlet (v, w) = b inx*v + y*w;;

    *

  • Review: cmp a blet cmp a b =let _ = print_string integer a = inlet _ = print_int a inlet _ = print_newline () inlet _ = print_string integer b = inlet _ = print_int b inlet _ = print_newline () inlet msg = if a > b then a is greater than belse if b > a then b is greater than aelse a and b are equalin print_string msg;;*

  • Review: Recursive functionDefine a function sum that does the followingSum type: int -> intAssume n >= 1Return the sum of 1 to nlet rec sum n =if n = 1 then 1else sum (n-1) + n;;*

  • Review: Recursive functionThe body of a recursive function consists of three componentsif then else Or,if not then else E.g.,if n = 1 then 1 else sum (n-1) + nOrif n > 1 then sum (n-1) + n else 1

    *

  • Review: Recursive functionThere must be a , otherwise infinite loop!let rec sum n = sum (n-1) + n;;The argument value must change, otherwise infinite loop!let rec sum n =if n=1 then 1 else sum n + n;;

    *

  • Exercise: sum_even nWrite a recursive function sum_even n that does the followingn, an argument, is an even number.If n is an odd or negative number, then print argument error.. and return -1sum_even n computes the sum of even numbers up to n, i.e., 2, 4, 6, , n

    *

  • Exercise: sum_even nlet rec sum_even n =if n < 0 || n mod 2 0 thenlet _ = print_string argument error.. in -1else if n = 0 then 0else sum_even (n-2) + n;;*

  • Recursive function that calls itself multiple timesRecursive function can call itself multiple times within the functionE.g., Fibonacci sequence ( )1, 1, 2, 3, 5, 8, 13, 21, Fn = Fn-1 + Fn-2F2 = F1 = 1*

  • Recursive function for Fibonacci sequenceInput and output type: int - > intArgument is assumed to be >= 1fib n returns Fn in the fibonacci sequence

    let rec fib n = if n > 2 then fib (n-1) + fib (n-2)else 1;;*

  • Recursive function for Fibonacci sequencelet rec fib n = if n > 2 then fib (n-1) + fib (n-2)else 1;;

    Fib calls itself twice - fib (n-1) and fib (n-2), but the arguments will be decreased by one or two each time it is called, thus No infinite loop!*

  • Recursive function with unchanging argumentConsider the following sequencea1 = 1an = k an-1 + 1Type of f: int -> intThe argument n is assumed to be >=1f n return anLet rec f n =if n > 1 then k * (f (n-1) ) + 1else 1;;*

  • Recursive function with unchanging argumentQ: what would be the output?let k = 3;;let rec f n =if n > 1 then k * (f (n-1) ) + 1else 1;;f 3;;In f, k is fixedWhen f is defined, k=3. Thus, an = 3an + 1If we want an=5an+1, then function f cannot be used.How to let k be variable?*

  • Recursive function with unchanging argumentDefine f_gen that has a variable klet f_gen k n =if n > 1 then k * (f_gen k (n-1) ) + 1else 1;;You can call f_gen with a variable k, thus k is set at the time f is called rather than f is defined.But, note that the value k is not changing when f_gen is called inside f_gen. Only the argument n is changing.This is ok because k is not used in the terminal condition.*

  • Recursive function with unchanging argumentDefine f_full that has the following sequencean = k an-1 + pa1 = qlet f_gen k p q n =if n > 1 then k * (f_gen k p q (n-1) ) + pelse q;;*

  • Recursive function having multiple outputsExample sequencean = 2an-1 + bn-1bn = an-1 + 2bn-1a1 = pb1 = qDefine f_pair for the above sequence using tupleWhy use tuple?*

  • Recursive function having multiple outputsExample sequencean = 2an-1 + bn-1bn = an-1 + 2bn-1a1 = pb1 = qType of f_pair: int -> int * int (or, int -> (int * int))f_pair n returns (an, bn)

    *

  • Recursive function having multiple outputsExample sequencean = 2an-1 + bn-1bn = an-1 + 2bn-1a1 = pb1 = qlet rec f_pair n =If n > 1 thenLet (a, b) = f_pair (n-1) in (2*a + b, a + 2*b)Else(p, q);;

    *

  • Recursive function having multiple variable argumentsHow to compute the GCD (Greatest Common Divisor) () of two values a and b?E.g., a=12 and 6=18, gcd is 6Euclidean Algorithm: Keep subtracting smaller value from larger value until two values become equalLet rec gcd a b =If a > b then gcd (a-b) bElse if a < b then gcd a (b-a)Else a;;*

    *