# F#柯里化和递归

>//没有柯里化，使用lambda表达式List.iter (fun i -> printfn "%d" i) [1 .. 3];;123val it : unit = ()>//使用printfn 柯里化List.iter (printfn "%d") [1 .. 3];;123val it : unit = ()

>let PowerFunc temp = (fun i -> temp ** i);;//求几的几次方函数val PowerFunc : float -> float -> float>let power1 = PowerFunc 2.0;; //调用PowerFunc返回一个函数val power1 : (float -> float)>power1 3.0;; //求3的2次方val it : float = 8.0;;>let power2 = PowerFunc 3.0;; //调用PowerFunc返回一个函数val power2 : (float -> float)>power2 2.0;; //求3的2次方val it : float = 9.0;;

>//定义一个递归函数let rec factorial x =    if x <= 1 then        1    else        x * factorial ( x - 1);;val factorial : int -> int>//调用factorial 5;;val it : int = 120

>//for 循环函数let rec forLoop body times =    if times <=0 then       ()    else       body()       forLoop body (times - 1)//while looplet rec whileLoop predicate body =    if predicate() then       body()       whileLoop predicate body    else       ();;val forLoop : (unit -> unit) -> int -> unitval whileLoop : (unit -> bool) -> (unit -> unit) -> unit> forLoop (fun () -> printfn "Looping...") 3;;//调用for loopLooping...Looping...Looping...val it : unit = ()>open SystemwhileLoop(fun () -> DateTime.Now.DayOfWeek <> DayOfWeek.Saturday)(fun () -> printfn "I wish it were the weekend...");;I wish it were the weekend...I wish it were the weekend...val it : unit = ()

posted @ 2011-09-08 18:09  carsick_cars  阅读(527)  评论(0编辑  收藏