List Info

Thread: Re: "ocaml_beginners"::[] How to properly write this ?




Re: "ocaml_beginners"::[] How to properly write this ?
country flaguser name
France
2007-08-20 01:04:22

Hi Jon,

My answer to your message doesn't appear on the list.
Rich confirmed that Yahoo Groups seems to be broken for a while.
So I sent it to you directly and forward it to Beginners-list.
----------------------------------------------------------

> # open List;;
> # let cons h t = h::t;;
> # let rec combinations = function
> | [] | [[]] -> [[]]
>; | []::ts -> combinations ts
> | hs::ts -> flatten (map (fun h -> map (cons h) (combinations ts)) hs);;

Hi Jon !

Thanks a lot for this piece of code that gives me to think !
This list of list of int lists, that are increased by one element, is flattenned and flattenned again up to the ultimate list of combinations. It was not so obvious to understand for me...
But that is short and efficient, works well with all I tried.

However, I do not see the aim of the case :
| []::ts -> combinations ts
How [] could appear in the list of integer lists ?
Are the first two | [] | [[]] not sufficient to cover all the degenerated inputs ?

If I've a bit understood how "combinations" works, it is not possible to hack it in order to pass a function that could take each combination as parameter. ( Because the complete lists arrives together when things come back to the unnested "combinations" ).
The interest of applying a function of successive combinations could be to be able to raise an exception to cut the combinations generation.

Best regards,

Fabrice

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )