Re: Page 76 of OCaml by John Whitington ???

"'Mr. Herr' [email protected] [ocaml_beginners]" <[email protected]> Fri, 25 Mar 2016 21:34:41 +0100
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>

On 25.03.2016 19:44, Douglas Lewit [email protected] [ocaml_beginners] wrote:
>  
> Well my exploration of LISP will have to wait, otherwise I'll be mediocre at many
> languages rather than really good at just a few!  Changing the topic a bit, I'm
> really struggling with something here.  If you look at the 99 Problems in Ocaml
> page, there's a problem that requires you to write a pack function that basically
> does this:
>
> pack [1; 1; 1; 1; 2; 2; 2; 2; 3; 3] ;;
>
> [[1; 1; 1; 1]; [2; 2; 2; 2]; [3; 3]];
>
> Just when I thought I was getting good at Ocaml I found a problem that has truly
> humbled me.  I've written a few different solutions.  Most compile but will not
> produce the desired result.  I looked at the proposed solution but had a really
> hard time tracing through it because in some cases the number of required arguments
> seems disjoint.  Here's the copy and paste of the proposed solution:
>
> Okay... confusion here!  First off look at */rec aux current acc/* ( why can't
> people just call it "accumulator" to make tracing easier?! )  So this recursive
> function aux ( or auxiliary ) accepts 2 arguments, "current" and "acc", right?  But
> then later on in the code it after the "then" statement, it appears that aux
> accepts 3 arguments, *( a :: current ) acc t*.  And then later on in the code we
> see aux [ ] [ ] list, which again leads to the idea that aux accepts 3 arguments,
> but in the declaration of aux, the aux function accepts only 2 arguments!  Okay, I
> am officially confused!  Why is the number of parameters changing like that.  Does
> it have something to do with currying or partial function application?  ( But with
> partial function application the number of parameters is less rather than more than
> the original number of parameters.... I thought? )
>
> If anyone can enlighten me on how to trace through this mess, I would be very
> grateful!  
>
> One last question.  With regard to recursive functions that create ref
> variables.... are those ref variables reinitialized every time the function is
> called?  I imagine they should be and would be, but.... I don't know.  I'll have to
> experiment with that one and figure it out.
>
>
> |# let pack list = let rec aux current acc = function | [] -> [] (* Can only be
> reached if original list is empty *) | [x] -> (x :: current) :: acc | a :: (b :: _
> as t) -> if a = b then aux (a :: current) acc t else aux [] ((a :: current) :: acc)
> t in List.rev (aux [] [] list);; val pack : 'a list -> 'a list list = <fun>|
>

Every time you see " function " you know this takes one (1) argument.
Every time you see "fun" you know it takes 1..n arguments, which is the reason
  you have to bind names to the parameters.
Behind the scene it is always "function".

|let rec aux current acc = function |

is equivalent to
 

|let rec aux current acc ls = match ls with|

with the advantage that the first does not unnecessarily bind a name to be used in
pattern matching.

/Str.