Sorry, an error occurred while loading the content.

## Polymorphic lists ?

Expand Messages
• Hi List, I ve searched for this in OCaml lists subjects and googled without true success ( The link : http://www.okmij.org/ftp/Scheme/misc.html referred to a
Message 1 of 5 , Sep 1, 2008
• 0 Attachment
Hi List,

I've searched for this in OCaml lists subjects and googled without true success ( The link :
http://www.okmij.org/ftp/Scheme/misc.html
referred to a Nicolas Pouillard example of polymorphic list in OCaml but I didnt understood a lot.)

In fact, the encountered problem is simple and somewhat recalls the topic about compilation warning posted yesterday for which Martin Jambon gave several solutions :

Please consider we have several lists of any types.

let il = [1; 2; 3]
let cl = ['q'; 'w']
let sl = ["er"]

To get there lengths, is there something better than to write :

let ill = List.length il
let cll = List.length cl
let sll = List.length sl

In other words, is it a mean to factor the repeated :

let ... = List.length ...

(for the case we have 20 lists of any kind) ?

Maybe polymorphic lists aren't necessary here ?
But I do not see how handling the question with tuples.

Thanks,

Fabrice
• Hi, I guess we can create a enum type to include all these lists into one list. type lists = IntList of int list | CharList of char list | StringList of
Message 2 of 5 , Sep 1, 2008
• 0 Attachment
Hi,
I guess we can create a enum type to include all these lists into one list.

type lists = IntList of int list | CharList of char list | StringList of
string list;;

then we could put il, cl, and sl into one list like

[IntList il; CharList cl; StringList sl]

Next, to define a length function for the lists type is simple.

I think enum type is a easy way to put items with different type into one
list, but I don't know
if there's a better way. Anyway, I am a beginner too.

On Mon, Sep 1, 2008 at 11:41 PM, Fabrice Marchant <
fabrice.marchant@...> wrote:

> Hi List,
>
> I've searched for this in OCaml lists subjects and googled without true
> success ( The link :
> http://www.okmij.org/ftp/Scheme/misc.html
> referred to a Nicolas Pouillard example of polymorphic list in OCaml but I
> didnt understood a lot.)
>
> In fact, the encountered problem is simple and somewhat recalls the topic
> about compilation warning posted yesterday for which Martin Jambon gave
> several solutions :
>
> Please consider we have several lists of any types.
>
> let il = [1; 2; 3]
> let cl = ['q'; 'w']
> let sl = ["er"]
>
> To get there lengths, is there something better than to write :
>
> let ill = List.length il
> let cll = List.length cl
> let sll = List.length sl
>
> In other words, is it a mean to factor the repeated :
>
> let ... = List.length ...
>
> (for the case we have 20 lists of any kind) ?
>
> Maybe polymorphic lists aren't necessary here ?
> But I do not see how handling the question with tuples.
>
> Thanks,
>
> Fabrice
>
>
>

[Non-text portions of this message have been removed]
• Thanks a lot for your help, On Tue, 2 Sep 2008 13:21:01 +0800 ... I see : that s a way to allow polymorphism in a predefined and restricted field of types. ...
Message 3 of 5 , Sep 1, 2008
• 0 Attachment
Thanks a lot for your help,

On Tue, 2 Sep 2008 13:21:01 +0800
"River Cheng" <rivercheng@...> wrote:

> I think enum type is a easy way to put items with different type into one
> list,

I see : that's a way to allow polymorphism in a predefined and restricted field of types.

> I guess we can create a enum type to include all these lists into one list.
>
> type lists = IntList of int list | CharList of char list | StringList of
> string list;;
>
> then we could put il, cl, and sl into one list like
>
> [IntList il; CharList cl; StringList sl]
>
> Next, to define a length function for the lists type is simple.

Something like this ?

let lists_length =
function
IntList l -> List.length l
| CharList l -> List.length l
| StringList l -> List.length l

allowing thus :

List.map
lists_length
[IntList [1; 2; 3]; CharList ['q'; 'w']; StringList ["er"]]

It works !

However it moves the problem : I now wonder if we could'nt factor the similar lines of 'lists_length'.
Another problem is I can't predefine a set of utilities that could solve once and for all the question (like in my recent question about a warning solved like this :
...
let map3 f (x1, x2, x3) = f x1, f x2, f x3
let map4 f (x1, x2, x3, x4) = f x1, f x2, f x3, f x4
...).

In the current question, we must adapt the enum of list types to each case to be able to cope with a new user enum type, for example.

Regards,

Fabrice
• ... Hash: SHA1 ... Generally, you ll find you rarely, if ever, need to put an int-list, a char-list and a string-list together into a larger container list.
Message 4 of 5 , Sep 2, 2008
• 0 Attachment
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 02 September 2008 02:22:15 am Fabrice Marchant wrote:
> Thanks a lot for your help,
>
> On Tue, 2 Sep 2008 13:21:01 +0800
>
> "River Cheng" <rivercheng@...> wrote:
> > I think enum type is a easy way to put items with different type into one
> > list,
>
> I see : that's a way to allow polymorphism in a predefined and restricted
> field of types.
>
> > I guess we can create a enum type to include all these lists into one
> > list.
> >
> > type lists = IntList of int list | CharList of char list | StringList of
> > string list;;
> >
> > then we could put il, cl, and sl into one list like
> >
> > [IntList il; CharList cl; StringList sl]
> >
> > Next, to define a length function for the lists type is simple.
>
> Something like this ?
>
> let lists_length =
> function
> IntList l -> List.length l
>
> | CharList l -> List.length l
> | StringList l -> List.length l
>
> allowing thus :
>
> List.map
> lists_length
> [IntList [1; 2; 3]; CharList ['q'; 'w']; StringList ["er"]]
>
> It works !
>
> However it moves the problem : I now wonder if we could'nt factor the
> similar lines of 'lists_length'. Another problem is I can't predefine a set
> of utilities that could solve once and for all the question (like in my
> recent question about a warning solved like this : ...
> let map3 f (x1, x2, x3) = f x1, f x2, f x3
> let map4 f (x1, x2, x3, x4) = f x1, f x2, f x3, f x4
> ...).
>
> In the current question, we must adapt the enum of list types to each case
> to be able to cope with a new user enum type, for example.
>
> Regards,
>
> Fabrice

Generally, you'll find you rarely, if ever, need to put an int-list, a
char-list and a string-list together into a larger container list. Your
question is synthetic (but interesting).

My suggestion is two fold:

1) Just try working with OCaml and write some simple toy projects. You'll
quickly realize you never use polymorphic lists. It's simply never needed.
For example, if you're reading in a file with "name:length" entries you might
have (psuedocode):

type length = Feet of float | Meter of float
type entry = string * length

let parseLine line = ...

let entries filename = List.map parseLine (readlines filename)

2) If you really want polymorphic lists, there are a variety of techniques.
For example you could use objects:

class lengthable (l:'a list) = object
method length () = List.length l
end

let somelists = [ new lengthable [1; 2; 3];
new lengthable ['q'; 'w'];
new lengthable ["er"];
]
let lengths = List.map (fun x -> x#length ()) somelists

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIvSL0fIRcEFL/JewRAjqcAJ94mHw+t/O62IYyBkY9IOaNk2r8wACgvV5s
MW4XDZwOqR9bTRyjqGGdgos=
=evfT
-----END PGP SIGNATURE-----
• On Tue, 2 Sep 2008 07:26:41 -0400 ... Thanks ! ... This problem arose trying to rewrite an old C project in OCaml. Here is the code snippet to show where
Message 5 of 5 , Sep 2, 2008
• 0 Attachment
On Tue, 2 Sep 2008 07:26:41 -0400
Peng Zang <peng.zang@...> wrote:

> Generally, you'll find you rarely, if ever, need to put an int-list, a
> char-list and a string-list together into a larger container list. Your
> question is synthetic (but interesting).
Thanks !

> My suggestion is two fold:
>
> 1) Just try working with OCaml and write some simple toy projects. You'll
> quickly realize you never use polymorphic lists. It's simply never needed.

This problem arose trying to rewrite an old C project in OCaml. Here is the code snippet to show where things come from :

char* opCodeStr[] ={
"DAT",
"MOV",
"ADD", "SUB", "MUL", "DIV", "MOD",
"JMP", "SEQ", "SNE", "JMZ", "JMN", "DJN", "CMP", "SLT",
"SPL",
"NOP",
"LDP", "STP"
};

char* modifierStr[] ={
"A", "B", "AB", "BA", "F", "X", "I"
};

'#',
'\$',
'*', '@',
'{', '<',
'}', '>'
};

long OPCODE_N =ARRAYSIZE(opCodeStr),
MODIFIER_N =ARRAYSIZE(modifierStr),

typedef struct{
opCode oc;
modifier m;
amB;
}* RedIns;

/* Computes a different number Nbr for each Redcode instruction :
*/
long RedInsNbr(RedIns RI){

}

> For example, if you're reading in a file with "name:length" entries you might
> have (psuedocode):
>
> type length = Feet of float | Meter of float
> type entry = string * length
>
> let parseLine line = ...
>
> let entries filename = List.map parseLine (readlines filename)
Yes, based on variants, a bit as River Cheng explained to me.

> 2) If you really want polymorphic lists, there are a variety of techniques.
> For example you could use objects:
>
> class lengthable (l:'a list) = object
> method length () = List.length l
> end
>
> let somelists = [ new lengthable [1; 2; 3];
> new lengthable ['q'; 'w'];
> new lengthable ["er"];
> ]
> let lengths = List.map (fun x -> x#length ()) somelists

It's a pity I wanted to forget objects, for a time, because I see that they seem to constitute indeed a perfect solution to this polymorphism question !

Regards,

Fabrice
Your message has been successfully submitted and would be delivered to recipients shortly.