List.flatten....
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Sat, 2 Apr 2016 00:32:54 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJQoY4jHyDrsDeEDXMbn7tZgdGDoGWETd5CayruMRrQwcQ@mail.gmail.com> |
Well I just discovered List.flatten.... AFTER I created my own implementation of the flatten function! Well I thought this function was actually pretty good until I found out that Ocaml's developers created a builtin function that does exactly the same thing! Oh well.... I just reinvented the wheel. ( But it's MY wheel, so of course I'm proud of it. ) let rec makeOneListFromMany nested_list = match nested_list with |[ ] -> [ ] |head :: tail -> match head with |[ ] -> makeOneListFromMany tail |headOfHead :: tailOfTail -> headOfHead :: ( makeOneListFromMany [tailOfTail] ) @ ( makeOneListFromMany tail ) ;; I think I read somewhere that the cons operator :: is more efficient than the append operator @, so whenever possible it's better to use cons rather than append. Well perhaps, but I think removing the append operator from the language would be like handicapping one of a horse's 4 legs. Can the horse still run? Well yes, sort of, but it's going to be difficult for the horse and the horse won't run as fast as before. Best, Douglas.