0

I'm going through the 99 problems on OCaml.org and I noticed that the solutions to the problems in many cases (e.g. problems 9, 10, 11, 12 to name some) follow the pattern:

let rev list =
    let rec aux = function
       | [] -> []
       | h :: t -> h :: (aux t)
    in
    List.rev (aux list)

rather then simply gathering the elements with @:

let rec rev = function
    | [] -> []
    | h :: t -> (rev t) @ [h]

(The above example is trivially simplified and stupid, used just for illustration.)

Is there any reason why using :: and List.rev is better, then using @ to append elements?

Tim
  • 7,075
  • 6
  • 29
  • 58
  • Does it apply to OCaml as well? – Tim May 08 '20 at 14:54
  • Yep, except for the syntax. The actual data structure is the same across pretty much any functional language. [I've actually written an answer on this already for Reason, but couldn't find it initially](https://stackoverflow.com/a/49053903/7943564). I'll translate it to OCaml and post it as an answer here in a bit. – glennsl May 08 '20 at 15:15
  • Oh, well I can't post an answer to a closed question, but I've updated the answer linked above with OCaml syntax. – glennsl May 08 '20 at 15:33

0 Answers0