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

"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Fri, 25 Mar 2016 18:54:57 +0000 (UTC)
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Inline. 

    On Friday, March 25, 2016 2:44 PM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> 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? )
aux does take three parameters; the (a::current) is evaluated, and is the type of a list with head pointing to a. It's as if this were written:
let l = a::current inaux l acc t

The key is in knowing the semantics and syntactic sugar that function represents. The function keyword is not the fun keyword, and there's a reason they are spelled uniquely. Function is actually short for something along the lines of "fun x -> match x with"; you can give it an anonymous parameter and it will match over it.
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.
If they are local to the scope, yes. If they are captured by the block structure then no. Here are some examples:let rec local_example param1 param2 param3 =    let local_ref = abc in   blah blah  computation; evaluation of local_example once again 

let pack list =   let ref not_local = xyz in  let rec captures_not_local = ...

# 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>
On Thu, Mar 24, 2016 at 4:05 PM, Kenneth Miller [email protected] [ocaml_beginners] <[email protected]> wrote:

     LISP is awesome because it allows you to generate code that fits what you want.

The way I understand it is, if you're familiar with what ppx is and does, imagine having a language that's built around making code that can fit into things like that - when you finish a solution, you can annotate it onto anything basically. It's like generics or type inference, but with whole modules and data structures instead of just a single function. 

    On Thursday, March 24, 2016 5:02 PM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
 

     Thanks Hendrik.  I'll have to find that paper by Peter Landin.  The author's name is strongly familiar, but right now I can't quite place it.  My AI professor said that LISP and other functional programming languages are great for AI, but I didn't quite get his explanation.  ( And to be fair, his explanation was pretty short and quick. )  Something about how AI requires the manipulation of symbolic values ( as in computer algebra ) and for that sort of thing functional programming is better suited than other programming paradigms.
On Thu, Mar 24, 2016 at 3:07 PM, Hendrik Boom [email protected] [ocaml_beginners] <[email protected]> wrote:

     On Thu, Mar 24, 2016 at 11:49:44AM -0500, Douglas Lewit [email protected] [ocaml_beginners] wrote:

> 
> P.S. I almost forgot. Is LISP considered the great granddaddy of all
> functional programming languages? Is it correct to say that OCaml is
> descended from LISP? Thanks for the feedback.

I'd say so, yes.

Since you are asking about history, perhaps you'd like some:

One of the important papers, while not explicitly about functional 
programming, that liberated functional programming from LISP was
"The Next 700 Programming Languages", by Peter Landin.

-- hendrik
   

  

      

  #yiv8318585178 #yiv8318585178 -- #yiv8318585178ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv8318585178 #yiv8318585178ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv8318585178 #yiv8318585178ygrp-mkp #yiv8318585178hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv8318585178 #yiv8318585178ygrp-mkp #yiv8318585178ads {margin-bottom:10px;}#yiv8318585178 #yiv8318585178ygrp-mkp .yiv8318585178ad {padding:0 0;}#yiv8318585178 #yiv8318585178ygrp-mkp .yiv8318585178ad p {margin:0;}#yiv8318585178 #yiv8318585178ygrp-mkp .yiv8318585178ad a {color:#0000ff;text-decoration:none;}#yiv8318585178 #yiv8318585178ygrp-sponsor #yiv8318585178ygrp-lc {font-family:Arial;}#yiv8318585178 #yiv8318585178ygrp-sponsor #yiv8318585178ygrp-lc #yiv8318585178hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv8318585178 #yiv8318585178ygrp-sponsor #yiv8318585178ygrp-lc .yiv8318585178ad {margin-bottom:10px;padding:0 0;}#yiv8318585178 #yiv8318585178actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv8318585178 #yiv8318585178activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv8318585178 #yiv8318585178activity span {font-weight:700;}#yiv8318585178 #yiv8318585178activity span:first-child {text-transform:uppercase;}#yiv8318585178 #yiv8318585178activity span a {color:#5085b6;text-decoration:none;}#yiv8318585178 #yiv8318585178activity span span {color:#ff7900;}#yiv8318585178 #yiv8318585178activity span .yiv8318585178underline {text-decoration:underline;}#yiv8318585178 .yiv8318585178attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv8318585178 .yiv8318585178attach div a {text-decoration:none;}#yiv8318585178 .yiv8318585178attach img {border:none;padding-right:5px;}#yiv8318585178 .yiv8318585178attach label {display:block;margin-bottom:5px;}#yiv8318585178 .yiv8318585178attach label a {text-decoration:none;}#yiv8318585178 blockquote {margin:0 0 0 4px;}#yiv8318585178 .yiv8318585178bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv8318585178 .yiv8318585178bold a {text-decoration:none;}#yiv8318585178 dd.yiv8318585178last p a {font-family:Verdana;font-weight:700;}#yiv8318585178 dd.yiv8318585178last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv8318585178 dd.yiv8318585178last p span.yiv8318585178yshortcuts {margin-right:0;}#yiv8318585178 div.yiv8318585178attach-table div div a {text-decoration:none;}#yiv8318585178 div.yiv8318585178attach-table {width:400px;}#yiv8318585178 div.yiv8318585178file-title a, #yiv8318585178 div.yiv8318585178file-title a:active, #yiv8318585178 div.yiv8318585178file-title a:hover, #yiv8318585178 div.yiv8318585178file-title a:visited {text-decoration:none;}#yiv8318585178 div.yiv8318585178photo-title a, #yiv8318585178 div.yiv8318585178photo-title a:active, #yiv8318585178 div.yiv8318585178photo-title a:hover, #yiv8318585178 div.yiv8318585178photo-title a:visited {text-decoration:none;}#yiv8318585178 div#yiv8318585178ygrp-mlmsg #yiv8318585178ygrp-msg p a span.yiv8318585178yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv8318585178 .yiv8318585178green {color:#628c2a;}#yiv8318585178 .yiv8318585178MsoNormal {margin:0 0 0 0;}#yiv8318585178 o {font-size:0;}#yiv8318585178 #yiv8318585178photos div {float:left;width:72px;}#yiv8318585178 #yiv8318585178photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv8318585178 #yiv8318585178photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv8318585178 #yiv8318585178reco-category {font-size:77%;}#yiv8318585178 #yiv8318585178reco-desc {font-size:77%;}#yiv8318585178 .yiv8318585178replbq {margin:4px;}#yiv8318585178 #yiv8318585178ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv8318585178 #yiv8318585178ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv8318585178 #yiv8318585178ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv8318585178 #yiv8318585178ygrp-mlmsg select, #yiv8318585178 input, #yiv8318585178 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv8318585178 #yiv8318585178ygrp-mlmsg pre, #yiv8318585178 code {font:115% monospace;}#yiv8318585178 #yiv8318585178ygrp-mlmsg * {line-height:1.22em;}#yiv8318585178 #yiv8318585178ygrp-mlmsg #yiv8318585178logo {padding-bottom:10px;}#yiv8318585178 #yiv8318585178ygrp-msg p a {font-family:Verdana;}#yiv8318585178 #yiv8318585178ygrp-msg p#yiv8318585178attach-count span {color:#1E66AE;font-weight:700;}#yiv8318585178 #yiv8318585178ygrp-reco #yiv8318585178reco-head {color:#ff7900;font-weight:700;}#yiv8318585178 #yiv8318585178ygrp-reco {margin-bottom:20px;padding:0px;}#yiv8318585178 #yiv8318585178ygrp-sponsor #yiv8318585178ov li a {font-size:130%;text-decoration:none;}#yiv8318585178 #yiv8318585178ygrp-sponsor #yiv8318585178ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv8318585178 #yiv8318585178ygrp-sponsor #yiv8318585178ov ul {margin:0;padding:0 0 0 8px;}#yiv8318585178 #yiv8318585178ygrp-text {font-family:Georgia;}#yiv8318585178 #yiv8318585178ygrp-text p {margin:0 0 1em 0;}#yiv8318585178 #yiv8318585178ygrp-text tt {font-size:120%;}#yiv8318585178 #yiv8318585178ygrp-vital ul li:last-child {border-right:none !important;}#yiv8318585178