Re: Page 76 of OCaml by John Whitington ???
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Mon, 28 Mar 2016 23:35:11 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJQx-XAir6-vreS5PXZSzE7h-TCfD8vV-Z7puQrmQj+s7Q@mail.gmail.com> |
Cool! I like it! I haven't played with Haskell before, but supposedly ( based on what I read on the web ) Haskell has ZERO imperative components. It's 100% functional, but that could be too much of a good thing. It's kind of nice when a language supports multiple paradigms. Not every hammer is ideal for every nail. I gave Ocaml a vacation for a couple days, and decided to try to implement the packing function in Ruby. Voila! It worked. Here's what I came up with. It's recursive, and may have a little bit of that Ocaml "flavor" to it. *# Pack consecutive duplicates of list elements into sublists.* *def pack( array )* * def pack_( array, return_list, sub_list )* * if array == [ ] then * * return return_list* * else* * if array[0] == array[1] then * * sub_list.push( array[0] )* * pack_( array[1...array.length], return_list, sub_list )* * else* * sub_list.push( array[0] )* * return_list.push( sub_list )* * pack_( array[1...array.length], return_list, [ ] )* * end* * end* * end* * pack_( array, [ ], [ ] )* * end* ( Sorry, but the indentations got lost in the copy and paste. Not a problem. The Ruby interpreter doesn't read white space. Python does, but not Ruby. ) On Mon, Mar 28, 2016 at 7:12 AM, Kenneth Miller [email protected] [ocaml_beginners] <[email protected]> wrote: > > > Right - almost always, if you're using references in a strongly typed > functional language, you're going against the idioms and best strengths of > the language. It can be hard to think of everything as having a recursive > solution, especially since it can often have non-obvious implementation > when you first start. Frequently, new functional programmers find > themselves repeating the habits they've been taught in imperative > languages, and those can be hard to unlearn. > > And thank you for your kind words! I'm honored, but I'm just an avid > learner with a master's; universities wouldn't accept me. I think I'll > pursue a PhD soon, but I have to finish my obligations with my current line > of work first. Also, Dr. Sleator's fix of your function clearly illustrates > what I've said above while also nudging you away from using references. > Thanks Dr. Sleator. > > > > On Sunday, March 27, 2016 10:21 PM, "[email protected] > [ocaml_beginners]" <[email protected]> wrote: > > > > Part of the problem is that the "reference solution" is incomprehensible. > Here's how I did it: > > let pack li = > let rec grab_block li ac = > (* Grab a block of equal elements from li and put them into ac. > Return the pair (li,ac). *) > match li with > | [] -> ([],ac) > | x::tail -> > if ac=[] || x = List.hd ac then grab_block tail (x::ac) > else (li, ac) > in > let rec loop li ac = if li = [] then List.rev ac else > let (rest,bl) = grab_block li [] in > loop rest (bl::ac) > in > loop li [] > > I think this is much easier to understand, and just as efficient. > > ---DS > > > >