Recursion 跟踪Ocaml递归函数

Recursion 跟踪Ocaml递归函数,recursion,ocaml,trace,Recursion,Ocaml,Trace,我在跟踪此代码时遇到问题(正确): 我的追踪不正确,但我不确定出了什么问题: prepend ([]::2::[]::2::3::[]::[]) 1 = 1::[]::prepend (2::[]::2::3::[]::[]) 1 = 1::[]::1::2::prepend([]::2::3::[]::[]) 1 = 1::[]::1::2::1::[]::prepend(2::3::[]::[]) 1 --> This is incorrect because then it com

我在跟踪此代码时遇到问题(正确):

我的追踪不正确,但我不确定出了什么问题:

prepend ([]::2::[]::2::3::[]::[]) 1 =
1::[]::prepend (2::[]::2::3::[]::[]) 1 =
1::[]::1::2::prepend([]::2::3::[]::[]) 1 =
1::[]::1::2::1::[]::prepend(2::3::[]::[]) 1 --> 
This is incorrect because then it comes out as [1] ; [1;2;1]
when it should be [1]; [1;2] ; [1;2;3]

::
运算符是非关联的,即
(a::b)::c
a::(b::c)
不同。因此,您应该使用括号来跟踪子列表

prepend ([] :: (2 :: []) :: (2 :: 3 :: []) :: []) 1 =>
(1 :: []) :: prepend ((2 :: []) :: (2 :: 3 :: []) :: []) 1 => 
(1 :: []) :: (1 :: 2 :: []) :: prepend ((2 :: 3 :: []) :: []) 1 => ...
也许你可以从那里拿走

prepend ([] :: (2 :: []) :: (2 :: 3 :: []) :: []) 1 =>
(1 :: []) :: prepend ((2 :: []) :: (2 :: 3 :: []) :: []) 1 => 
(1 :: []) :: (1 :: 2 :: []) :: prepend ((2 :: 3 :: []) :: []) 1 => ...