[erlang-questions] The reason for "no case clause matching" error?

Hynek Vychodil vychodil.hynek@REDACTED
Thu Jan 3 17:39:30 CET 2008


A little more efficient version:

-module(ppar).
-export([ppar/1]).

sp(Lhs, []) ->
   [[Lhs]];
sp(Lhs, Rhs) ->
   [ [Lhs|P] || P <- ppar(Rhs)].

ppar([]) ->
   [[[]]];
ppar([H]) ->
   [[[H]]];
ppar(Lst)  ->
   [SP ||
   N <- lists:seq(1,length(Lst)),
   SP <- begin
   		{Lhs, Rhs} = lists:split(N, Lst),
		sp(Lhs, Rhs)
	end].

On 1/1/08, Fredrik Svahn <fredrik.svahn@REDACTED> wrote:
> Hi,
>
>
> I cannot say I fully understand the error message. However the reference
> manual says:
>
> "List comprehensions are written with the following syntax:
> [Expr || Qualifier1,...,QualifierN]
>
>
>
>
> Expr is an arbitrary expression, and each Qualifier is either a generator or
> a filter.
>
>
>  A generator is written as:
>    Pattern <- ListExpr.
>  ListExpr must be an expression which evaluates to a list of terms.
> A filter is an expression which evaluates to true or false."
> Some of the Qualifiers you use in your list comprehension (e.g. Lhs =
> lists:sublist(Lst, 1, N)) are neither generators nor filters since they do
> not contain "<-" and evaluates to lists rather than true or false.
>
> I suggest rewriting the last function clause to:
>
> ppar(Lst)  ->
>    [SP ||
>    N <- lists:seq(1,length(Lst)),
>    SP <- sp(lists:sublist(Lst, 1, N), lists:sublist(Lst, N+1,
> length(Lst)))].
>
> or (to maintain the variables for clarity)
>
> ppar(Lst)  ->
>     [SP || N <- lists:seq(1,length(Lst)),
>        SP <- begin
>              Lhs = lists:sublist(Lst, 1, N),
>              Rhs = lists:sublist(Lst, N+1, length(Lst)),
>              sp(Lhs, Rhs)
>          end].
>
>
> Gives:
>
> 2> test4:ppar([2,2,3]).
> [[[2],[2],[3]],[[2],[2,3]],[[2,2],[3]],[[2,2,3]]]
>
> Hope this helps.
>
> BR /Fredrik
>
>
>
>  On Jan 1, 2008 1:08 PM, Yoel Jacobsen <yoel@REDACTED> wrote:
> >
> > Hello,
> >
> >
> > I want to implement some sort of list partitioning in Erlang.
> >
> >
> > The ppar function should work like that:
> >
> > ppar [2,2,3] -> [[[2],[2],[3]], [[2],[2,3]], [[2,2],[3]],[ [2,2,3]]]
> > ppar [2,2,3,4] -> [[[2],[2],[3],[4]], [[2],[2],[3,4]], [[2],[2,3],[4]],
> > [[2],[2,3,4]], [[2,2],[3],[4]], [[2,2],[3,4]], [[2,2,3],[4]], [[2,2,3,4]]]
> >
> >
> > This is the code I have written:
> >
> > -module(ppar).
> > -export([ppar/1]).
> >
> >
> > sp(Lhs, []) ->
> >    [[Lhs]];
> > sp(Lhs, Rhs) ->
> >    [lists:append([Lhs], P) || P <- ppar(Rhs)].
> >
> > ppar([]) ->
> >    [[[]]];
> > ppar([H|[]]) ->
> >    [[[H]]];
> > ppar(Lst)  ->
> >    [SP ||
> >    N <- lists:seq(1,length(Lst)),
> >    Lhs = lists:sublist(Lst, 1, N),
> >    Rhs = lists:sublist(Lst, N+1, length(Lst)),
> >    SP <- sp(Lhs, Rhs)].
> >
> > Yet, when I try it I get an error:
> >
> > 1> ppar:ppar([1,2,3]).
> > ** exception error: no case clause matching [1]
> >     in function  ppar:'-ppar/1-lc$^0/1-0-'/2
> >
> > Why is that?
> >
> > Yoel
> >
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


-- 
--Hynek (Pichi) Vychodil



More information about the erlang-questions mailing list