2012年11月25日

filter in Ocaml

摘要: 1 let filter p l = 2 let rec filter_int g ll rr = 3 match ll with 4 | [] -> rr 5 | h::r -> if (g h) then filter_int g r (h::rr) else filter_int g r rr 6 in 7 let rec map_int g ll rr = 8 match ll with 9 | [] -> rr10 | h::r -> map_int ... 阅读全文

posted @ 2012-11-25 19:04 mathlover 阅读(200) 评论(0) 推荐(0) 编辑

gcd, map for ocaml

摘要: gcd 的代码如下let gcd a b = let rec gcd_int x y = let r = x mod y in if r = 0 then y else gcd_int y r in if b = 0 then a else gcd_int a b ;;map的简单实现为let rec map f = [] -> [] |h::l -> f h :: map f l ;;这个实现不是尾递归的,对于长表来说可能会栈溢出,尾递归的版本为let map f l = let reverse lst r = match lst with ... 阅读全文

posted @ 2012-11-25 11:03 mathlover 阅读(223) 评论(0) 推荐(0) 编辑

导航