我是OCaml的新手,试图将List.append
作为学习练习。这是我有:List.rev表现奇怪吗?
let rec append a b =
match (List.rev a) with
[] -> b
| x:: xs -> append xs (x::b)
这似乎是工作,直到说法a
有两个以上的元素。例如:
# append [1;2] [3;4]
- : int list = [1; 2; 3; 4]
# append [1;2;3] [4;5;6]
- : int list = [2; 1; 3; 4; 5; 6]
这是怎么回事吗?我查过了,List.rev [1;2;3]
返回int list = [3; 2; 1]
。我知道我的实现是天真的,而不是懒惰的,但它似乎应该工作。
你使用'ocamldebug'调试器吗?你是否查看了* Ocaml *(在'ocaml-3.12/stdlib/list.ml'中)代码为'List.rev'的源代码[free as in speech]? – 2012-03-06 19:23:17
我现在在调试器中,但没有看到任何明显的线索。我确实查看了源代码并确信自己“List.rev”的实现应该像我期望的那样工作。你有其他想法吗? – Pygmalion 2012-03-06 19:30:35
在你的'append'或一些打印内容的开始处放置一个断点。 – 2012-03-06 19:34:01