caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Construct a list of records from several lists
@ 2013-09-05 16:38 Casey Basichis
  2013-09-05 16:46 ` John Whitington
  0 siblings, 1 reply; 4+ messages in thread
From: Casey Basichis @ 2013-09-05 16:38 UTC (permalink / raw)
  To: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 182 bytes --]

Hi,

I'm new to Ocaml.

I have five lists of ints, how can I can I combine them into a list of
records where the fields are filled with values from each of the lists?

Thanks,
Casey

[-- Attachment #2: Type: text/html, Size: 290 bytes --]

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

* Re: [Caml-list] Construct a list of records from several lists
  2013-09-05 16:38 [Caml-list] Construct a list of records from several lists Casey Basichis
@ 2013-09-05 16:46 ` John Whitington
  2013-09-05 17:15   ` Casey Basichis
  0 siblings, 1 reply; 4+ messages in thread
From: John Whitington @ 2013-09-05 16:46 UTC (permalink / raw)
  To: Casey Basichis; +Cc: OCaml Mailing List

Hi Casey,

Casey Basichis wrote:
> Hi,
>
> I'm new to Ocaml.

There is also this list, for beginners specifically:

http://groups.yahoo.com/neo/groups/ocaml_beginners/info

> I have five lists of ints, how can I can I combine them into a list of
> records where the fields are filled with values from each of the lists?

Do you mean that the lists are fixed-length, that you have a record type 
something like

type t = {a : int; b : int; c : int; d : int}

and you want to convert each list, which is something like

[1; 2; 3; 4]

into a record something like

{a = 1; b = 2; c = 3; d = 4}

?


In which case, you want to use pattern matching to write a function of type

int list -> t

which converts a (fixed-length) list to a thing of type t and then you 
can use List.map to deal with all your lists, producing a t list from an 
int list list.

If you meant something different entirely, please clarify!

With Thanks,

-- 
John Whitington
Director, Coherent Graphics Ltd
http://www.coherentpdf.com/


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

* Re: [Caml-list] Construct a list of records from several lists
  2013-09-05 16:46 ` John Whitington
@ 2013-09-05 17:15   ` Casey Basichis
  2013-09-05 17:30     ` John Whitington
  0 siblings, 1 reply; 4+ messages in thread
From: Casey Basichis @ 2013-09-05 17:15 UTC (permalink / raw)
  To: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1794 bytes --]

Sorry about the confusion. I'm taking from one element from each list per
record. The lists can all be arbitrarily long as long as they share the
same length.

lista = [1; 2; 3; 4; 5; 6; 3]
listb = [2; 5; 7; 8; 1; 2; 4]
listc = [4; 6; 8; 3; 5; 8; 2]

type t = {a : int; b: int; c: int}

{a=1; b=2; c=4}
{a=2; b=5; c=6}
{a=3; b=7; c=8}
{a=4; b=8; c=3}
...

I will subscribe to the beginners list as well.

Thanks,
Casey


On Thu, Sep 5, 2013 at 9:46 AM, John Whitington <john@coherentgraphics.co.uk
> wrote:

> Hi Casey,
>
>
> Casey Basichis wrote:
>
>> Hi,
>>
>> I'm new to Ocaml.
>>
>
> There is also this list, for beginners specifically:
>
> http://groups.yahoo.com/neo/**groups/ocaml_beginners/info<http://groups.yahoo.com/neo/groups/ocaml_beginners/info>
>
>
>  I have five lists of ints, how can I can I combine them into a list of
>> records where the fields are filled with values from each of the lists?
>>
>
> Do you mean that the lists are fixed-length, that you have a record type
> something like
>
> type t = {a : int; b : int; c : int; d : int}
>
> and you want to convert each list, which is something like
>
> [1; 2; 3; 4]
>
> into a record something like
>
> {a = 1; b = 2; c = 3; d = 4}
>
> ?
>
>
> In which case, you want to use pattern matching to write a function of type
>
> int list -> t
>
> which converts a (fixed-length) list to a thing of type t and then you can
> use List.map to deal with all your lists, producing a t list from an int
> list list.
>
> If you meant something different entirely, please clarify!
>
> With Thanks,
>
> --
> John Whitington
> Director, Coherent Graphics Ltd
> http://www.coherentpdf.com/
>
>


-- 
Casey James Basichis
Composer - Adventure Time - Cartoon Network
http://www.caseyjamesbasichis.com
caseybasichis@gmail.com
310.387.7540

[-- Attachment #2: Type: text/html, Size: 3886 bytes --]

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

* Re: [Caml-list] Construct a list of records from several lists
  2013-09-05 17:15   ` Casey Basichis
@ 2013-09-05 17:30     ` John Whitington
  0 siblings, 0 replies; 4+ messages in thread
From: John Whitington @ 2013-09-05 17:30 UTC (permalink / raw)
  To: Casey Basichis; +Cc: OCaml Mailing List

Hi,

Casey Basichis wrote:
>
> Sorry about the confusion. I'm taking from one element from each list
> per record. The lists can all be arbitrarily long as long as they share
> the same length.
>
> lista = [1; 2; 3; 4; 5; 6; 3]
> listb = [2; 5; 7; 8; 1; 2; 4]
> listc = [4; 6; 8; 3; 5; 8; 2]
>
> type t = {a : int; b: int; c: int}
>
> {a=1; b=2; c=4}
> {a=2; b=5; c=6}
> {a=3; b=7; c=8}
> {a=4; b=8; c=3}

So with

type t = {a : int; b : int; c : int}

So you want a function of type

int list -> int list -> int list -> t list

I left a few blanks (* ... *) for you to fill in:

let build_record p q r =
   (* Put something here to build a single record *)

let rec records_from_lists la lb lc =
   match la, lb, lc with
   | [], [], [] ->
       (* And something here to deal with the base case *)
   | x::xs, y::ys, z::zs ->
       build_record x y z :: records_from_lists xs ys zs
   | _ ->
       (* And something here to deal with malformed inputs *)

If you need any more help, I suggest we move it to the beginners list...

With Thanks,

-- 
John Whitington
Director, Coherent Graphics Ltd
http://www.coherentpdf.com/


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

end of thread, other threads:[~2013-09-05 17:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-05 16:38 [Caml-list] Construct a list of records from several lists Casey Basichis
2013-09-05 16:46 ` John Whitington
2013-09-05 17:15   ` Casey Basichis
2013-09-05 17:30     ` John Whitington

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).