5

我正在尝试通过解决Project Euler的问题 18来学习 Ocaml。我知道我想做什么,只是不知道该怎么做。

我有三个列表:

let list1 = [1;2;3;4;5];;
let list2 = [ 6;7;8;9];;
let line = [9999];;

我想将数字 list2 添加到 list1 中的最大相邻数字,IOW 我将添加 6+2、7+3、8+4 和 9+5 以获得列表 [8;10;12;14]。列表 line[] 是一个虚拟变量。

这是我的第三次尝试:

let rec meld3 l1 l2 accum =
   if List.length l2 = 1 then
     List.append accum [ (hd l2 + max (hd l1) (hd (tl l1)))]
else
    (
     List.append accum [ (hd l2 + max (hd l1) (hd (tl l1)))];
     meld3 (tl l1) (tl l2) accum ;
    )
;;

let fu = meld3 list1 list2 line ;;

List.iter print_int fu;;

运行此程序后,我希望 line = [9999;8;10;12;14] 而不是 line = [9999]。OTOH,fu 打印为 [999914]。

当我单步执行代码时,代码按预期执行,但没有任何变化;else 块中的累加器永远不会被修改。

我就是不懂这种语言。任何人都可以建议吗?

4

2 回答 2

9

好的,让我们分解你的代码。这是你的原件。

let rec meld3 l1 l2 accum =
   if List.length l2 = 1 then
     List.append accum [ (hd l2 + max (hd l1) (hd (tl l1)))]
else
    (
     List.append accum [ (hd l2 + max (hd l1) (hd (tl l1)))];
     meld3 (tl l1) (tl l2) accum ;
    )

我要做的第一件事是重写它,以便 Caml 程序员能够理解它,而无需更改任何计算。这主要意味着使用模式匹配而不是hdand tl。这种转变不是微不足道的。简化列表操作以更容易识别代码问题非常重要。这也使得这个函数在l2为空时失败更明显。

let rec meld3 l1 l2 accum = match l1, l2 with
| x1::x2::xs, [y] ->   (* here the length of l2 is exactly 1 *)
     List.append accum [ y + max x1 x2 ]
| x1::x2::xs, y::ys ->   (* here the length of l2 is at least 1 *)    
    ( List.append accum [ y + max x1 x2 ]
    ; meld3 (x2::xs) ys accum
    )

现在我认为你的困难的关键是对分号运算符的理解。如果我写 ( e1 ; e2 ),语义是e1被评估为副作用(think printf),然后e1 的结果被丢弃。我认为你想要的是让e1accum的结果成为递归调用的新值。因此,我们没有丢弃e1,而是将其设为参数(这是计算实际发生变化的关键步骤):

let rec meld3 l1 l2 accum = match l1, l2 with
| x1::x2::xs, [y] ->   (* here the length of l2 is exactly 1 *)
     List.append accum [ y + max x1 x2 ]
| x1::x2::xs, y::ys ->   (* here the length of l2 is at least 1 *)    
    ( 
      meld3 (x2::xs) ys (List.append accum [ y + max x1 x2 ])
    )

下一步是观察我们违反了不要重复自己的原则,我们可以通过将基本情况设为l2空来解决这个问题:

let rec meld3 l1 l2 accum = match l1, l2 with
| x1::x2::xs, [] ->   (* here the length of l2 is 0 *)
     accum 
| x1::x2::xs, y::ys ->   (* here the length of l2 is at least 1 *)    
    ( 
      meld3 (x2::xs) ys (List.append accum [ y + max x1 x2 ])
    )

然后我们清理一下:

let rec meld3 l1 l2 accum = match l1, l2 with
| _, [] -> accum 
| x1::x2::xs, y::ys -> meld3 (x2::xs) ys (List.append accum [ y + max x1 x2 ])

最后,重复调用append使代码成为二次方。这是一个累积参数的经典问题,并且有一个经典的解决方案:以相反的顺序累积答案列表:

let rec meld3 l1 l2 accum' = match l1, l2 with
| _, [] -> List.rev accum' 
| x1::x2::xs, y::ys -> meld3 (x2::xs) ys (y + max x1 x2 :: accum')

我已将名称更改accumaccum'; 对于以相反顺序排列的列表,素数是常规的。最后一个版本是我编译的唯一版本,我还没有测试任何代码。(我确实在我的另一个答案中测试了代码)。

我希望这个答案更有帮助。

于 2009-02-08T00:13:57.567 回答
5

Well, I think you haven't grasped the essence of functional programming: instead of calling List.append and throwing the value away, you need to pass that value as the parameter accum to the recursive call.

I would tackle this problem by decoupling the triangle geometry from the arithmetic. The first function takes two lists (rows of the triangle) and produces a new list of triples, each containing and element plus that element's left and right child. Then a simple map produces a list containing the sum of each element with its greater child:

(* function to merge a list l of length N with a list l' of length N+1,
   such that each element of the merged lists consists of a triple
     (l[i], l'[i], l'[i+1])
 *)

let rec merge_rows l l' = match l, l' with
  | [], [last] -> []   (* correct end of list *)
  | x::xs, y1::y2::ys -> (x, y1, y2) :: merge_rows xs (y2::ys)
  | _ -> raise (Failure "bad length in merge_rows")

let sum_max (cur, left, right) = cur + max left right

let merge_and_sum l l' = List.map sum_max (merge_rows l l')

let list1 = [1;2;3;4;5]
let list2 = [ 6;7;8;9]

let answer = merge_and_sum list2 list1

If you are working on Euler 18, I advise you to look up "dynamic programming".

于 2009-02-07T22:51:40.417 回答