caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Clarification of original post [still having problems]
@ 2003-04-20 21:55 Ryan Bastic
  2003-04-22 19:46 ` Brian Hurt
  2003-04-22 19:53 ` Karl Zilles
  0 siblings, 2 replies; 4+ messages in thread
From: Ryan Bastic @ 2003-04-20 21:55 UTC (permalink / raw)
  To: caml-list


Can anyone take some time and help a struggling newbie out? :-(

This stuff is still failing to parse right for my brain.

----- Original Message Follows -----
> Hello all,
>   A few weeks ago I posted a message asking how to create a program in
> OCaml to shuffle  vector of strings into an array of arrays. Had some
> problems understanding how to do it in Ocaml properly, and a kind soul
> posted a very elegant solution to the problem :-) unfortunately, i
> still can't understand some things from it.
> 
>   What follows is some code with comments on how it is expected to be
> used, and also
> where my confusion in the semantics of the code lay.
> 
>   (* Return first 'n' from input and the rest. *)
> let firstN n input =
>   let nInput = Array.length input in
>     if n >= nInput
>     then (input, [||])
>     else (Array.sub input 0 n, Array.sub input n (nInput - n))
> 
> let group n input =
>   let rec group' n input =
>     if Array.length input = 0 then []
>     else 
>       let (front, rest) = firstN n input in
>         (* the next line confuses me. i'm aware of :: being a list
> concatenation operator, but in this case, shouldn't group 'n rest be
> returning an array, because that's what firstN returns. I've
experimented in
> the REPL and had no luck in figuring it out. *)
>         front :: group' n rest
>   in
>     Array.of_list (group' n input)
> 

The specific problems that confuse me is that 

let rec group' ... in Array.of_list is all part of the same expression,
so how does Ocaml
let front :: group' n rest work?? firstN n input returns two arrays, and
as far as my
understanding goes, :: is a list concatenation operator. How then, does
this work?

Someone help! :)
 
> if you pop this into the toplevel, and do:
> 
> group 2 [|"Name1"; "Name2"; "Name3"; "Name4"; "Name5";|];;
> 
> you'll see the general functionality expected.
> 

-Ryan
http://malander.undrgnd.net

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Clarification of original post [still having problems]
  2003-04-20 21:55 [Caml-list] Clarification of original post [still having problems] Ryan Bastic
@ 2003-04-22 19:46 ` Brian Hurt
  2003-04-22 19:53 ` Karl Zilles
  1 sibling, 0 replies; 4+ messages in thread
From: Brian Hurt @ 2003-04-22 19:46 UTC (permalink / raw)
  To: Ryan Bastic; +Cc: caml-list

On Sun, 20 Apr 2003, Ryan Bastic wrote:

> 
> Can anyone take some time and help a struggling newbie out? :-(
> 
> This stuff is still failing to parse right for my brain.
> 
> ----- Original Message Follows -----
> > Hello all,
> >   A few weeks ago I posted a message asking how to create a program in
> > OCaml to shuffle  vector of strings into an array of arrays. Had some
> > problems understanding how to do it in Ocaml properly, and a kind soul
> > posted a very elegant solution to the problem :-) unfortunately, i
> > still can't understand some things from it.
> > 
> >   What follows is some code with comments on how it is expected to be
> > used, and also
> > where my confusion in the semantics of the code lay.
> > 
> >   (* Return first 'n' from input and the rest. *)
> > let firstN n input =
> >   let nInput = Array.length input in
> >     if n >= nInput
> >     then (input, [||])
> >     else (Array.sub input 0 n, Array.sub input n (nInput - n))
> > 
> > let group n input =
> >   let rec group' n input =
> >     if Array.length input = 0 then []
> >     else 
> >       let (front, rest) = firstN n input in
> >         (* the next line confuses me. i'm aware of :: being a list
> > concatenation operator, but in this case, shouldn't group 'n rest be
> > returning an array, because that's what firstN returns. I've
> experimented in
> > the REPL and had no luck in figuring it out. *)
> >         front :: group' n rest
> >   in
> >     Array.of_list (group' n input)
> > 
> 
> The specific problems that confuse me is that 
> 
> let rec group' ... in Array.of_list is all part of the same expression,
> so how does Ocaml
> let front :: group' n rest work?? firstN n input returns two arrays, and
> as far as my
> understanding goes, :: is a list concatenation operator. How then, does
> this work?

You can have lists of arrays just fine.  Which is what this function 
returns.  Basically, it (recursively) calls group', which returns a list 
of arrays.  When the recursive call returns, it prepends front to the list 
and returns the prepended-to list.

One trick I find myself doing a lot is to cut internal functions out into 
global functions to check their type signatures.  And given:
  let rec group' n input =
    if Array.length input = 0 then []
    else
      let (front, rest) = firstN n input in
        front :: group' n rest

Ocaml returns the type:
val group' : int -> 'a array -> 'a array list = <fun>

Which means a function that takes two arguments, an int and an 'a array, 
and returns a 'a array list.  group' returns a list- so prepending an 'a 
to the list makes perfect sense.

Does this help?

Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Clarification of original post [still having problems]
  2003-04-20 21:55 [Caml-list] Clarification of original post [still having problems] Ryan Bastic
  2003-04-22 19:46 ` Brian Hurt
@ 2003-04-22 19:53 ` Karl Zilles
  1 sibling, 0 replies; 4+ messages in thread
From: Karl Zilles @ 2003-04-22 19:53 UTC (permalink / raw)
  To: rbastic; +Cc: caml-list

Ryan Bastic wrote:
> The specific problems that confuse me is that 
> 
> let rec group' ... in Array.of_list is all part of the same expression,
> so how does Ocaml
> let front :: group' n rest work??

It is a recursive funtion call.  Is this clear?

> firstN n input returns two arrays, and  as far as my
> understanding goes, :: is a list concatenation operator. How then, does
> this work?

:: is a list building primitive.  If you have a list of integers (ilist) 
and you want to add an integer (newi) to the front, you would use:

newi :: ilist

So in general you add a foo to a foolist with (foo::foolist).

In your case, we are adding an array to a list of arrays.
front is the first array returned by the earlier call to firstN.
group' returns a list of arrays.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Clarification of original post [still having problems]
@ 2003-04-22 21:12 Ryan Bastic
  0 siblings, 0 replies; 4+ messages in thread
From: Ryan Bastic @ 2003-04-22 21:12 UTC (permalink / raw)
  To: caml-list, engstdad

[snip original message]

Thanks guys. I understood everything except that for some reason my
brain stopped working properly when it came to how the group' function
handled recursion... :-) Pal-Kristian Engstad also replied privately:
his explanation was a big help in figuring out.

Thanks again to everyone!
-Ryan Bastic

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-04-22 21:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-20 21:55 [Caml-list] Clarification of original post [still having problems] Ryan Bastic
2003-04-22 19:46 ` Brian Hurt
2003-04-22 19:53 ` Karl Zilles
2003-04-22 21:12 Ryan Bastic

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).