| Newsgroups |
gmane.comp.lang.ocaml.beginners |
| Message-ID |
<[email protected]> |
Have a second look at Chingfan Tsao post.
He gives you the solution.
Yes: l2 was renamed to accum. This is because it is an accumulator that
is growing during the calls, and returns the result at the end.
You'll notice that inside rev_list_tail_rec, a hidden function is
defined, and called. During this call, accum is initialized.
Best regards
Le 20/10/2014 17:56, Roelof Wobben [email protected] [ocaml_beginners] a
écrit :
>
> Oke,
>
> If I look it it seems to me that acc does the same as l2 on my programm.
> But does acc not have to be intialized on some way ?
>
> Sébastien
>
> then the outcome will be :
>
> string list = ["c"; "b"; "a"; "1"; "2"; "3"]
>
> so it reverse the second string and not the first one.
>
> Roelof
>
>
>
>
> Chingfan Tsao [email protected] [ocaml_beginners] schreef op
> 20-10-2014 15:55:
>> Hi,
>>
>> It is indeed a good way to solve it since it's tail recursion,
>> however, there's one more thing to do to make it easier, wrap it with
>> a more convenient function:
>>
>> let rev_list_tail_rec l =
>> let rec aux_rev l accum =
>> match l with
>> | [] -> accum
>> | h :: tl ->
>> aux_rev tl (h::accum)
>> in
>> aux_rev l []
>>
>> this way, you can just use rev_list_tail_rec l instead of having to
>> give one more [] as the initial value of the accumulator.
>>
>> 2014-10-20 18:56 GMT+08:00 Roelof Wobben [email protected]
>> <mailto:[email protected]> [ocaml_beginners]
>> <[email protected]
>> <mailto:[email protected]>>:
>>
>> Hello,
>>
>> I try the 99 ocaml problems and have solved the reverse a list
>> problem.
>>
>> My solution looks like this :
>>
>> let rec test l1 l2 =
>> match l1 with
>> | [] -> l2
>> | h :: t -> test t (h::l2)
>> ;;
>>
>> Is this a good way to solve it or are there things that can be
>> better ?
>>
>> Roelof
>>
>>
>>
>>
>> --
>> 曹竞帆
>> 哈尔滨工业大学计算机科学与技术专业本科在读
>> 哈尔滨工业大学IBMTC成员
>
>
--
Matthieu Dubuget
Guide d’autodéfense numérique : http://guide.boum.org