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

Andre Engels andreengels@REDACTED
Thu Jan 3 14:32:37 CET 2008


2008/1/1, Yoel Jacobsen <yoel@REDACTED>:
> 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?

You cannot use a pattern matching operator inside a list comprehension
(or at least, it does not do what you want it to do, it does not give
a syntax error, so apparently it means something, though I do not know
what). Thus,

Lhs = lists:sublist(Lst, 1, N),

is not correct syntax here.

Your program will work correctly if you bypass the variables Lhs and
Rhs as below:

ppar(Lst)  ->
   [SP ||
   N <- lists:seq(1,length(Lst)),
   SP <- sp(lists:sublist(Lst, 1, N), lists:sublist(Lst, N+1, length(Lst)))].

-- 
Andre Engels, andreengels@REDACTED
ICQ: 6260644  --  Skype: a_engels



More information about the erlang-questions mailing list