Re: reverse a list. is this a good solution

"Chingfan Tsao [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAOCiaG6e3hEGiezSGrTwFkbVJjExRzG2q0KGFDLO5e51efOh3w@mail.gmail.com>
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] [ocaml_beginners]
<[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成员
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.