Version française
Home     About     Download     Resources     Contact us    
Browse thread
recursion/iterator question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christian Stork <cstork@i...>
Subject: Re: [Caml-list] recursion/iterator question
On Mon, Apr 17, 2006 at 06:07:47PM +0100, Jon Harrop wrote:
> On Monday 17 April 2006 10:36, Christian Stork wrote:
> > Or, if you choose to represent triplets as lists of three elements, you can
> > generalize Jon's solution to

> >   let rec combs = function

> >       | (0, _) -> [[]]
> >       | (n, es) when n > List.length es -> []
> >       | (n, e::es) -> List.map (fun l -> e::l) (combs (n-1, es)) @ combs
> >       | (n, es)

> >   let triplets es = combs (3, es)

> > Question to the rest of the list:  The ocaml compiler complains with
> >   ...
> >   Warning P: this pattern-matching is not exhaustive.
> >   Here is an example of a value that is not matched:
> >   (1, [])
> >   (However, some guarded clause may match this value.)
> >   ...

> > Am I right to assume there's no way to get rid of this warning short of
> > disabling P-warnings on the command line?  (I can't list all the lacking
> > patterns since they depend on n, right?)

> The lacking patterns are covered by (_, []), so you probably want:

>   | 0, _ | _, [] -> [[]]

No, that would generate solutions shorter than the requested n elements.
What we want is something like

    | 1, []
    | 2, [] | 2, [_]
    | 3, [] | 3, [_] | 3, [_;_]
    ...
    | n, [] | n, [_] | n, [_;_] | ... | n, [_;...;_] (* n-1 times '_' *)
    -> []

Anyway, I think it's better not to use pattern matching but to use a
simple if-then-else to circumvent the warning.

-- 
Chris Stork   <>  Support eff.org!  <>   http://www.ics.uci.edu/~cstork/
OpenPGP fingerprint:  B08B 602C C806 C492 D069  021E 41F3 8C8D 50F9 CA2F