Erlang/OTP Forums

Author Message

<  Erlang-DACH  ~  Anfänger hat Problem mit Listen

Rainer
Posted: Tue Jun 08, 2010 7:01 am Reply with quote
Joined: 08 Jun 2010 Posts: 2 Location: Saarbrücken
Hallo zusammen,

ich habe gerade angefangen, mit Erlang zu spielen, und habe jetzt ein kleines Problem: ich bekomme einen Fehler, den ich mir nicht erklären kann. Hier zeige ich mal das Progrämmchen, das einen Bubble Sort implementieren soll ->

Code:

-module(sortieren).
-compile(export_all).

%

% Bubble Sort

bs(L) ->
  bs_h(L,[],true).

bs_h([],Alt,Ok) when Ok == true ->
  Alt;
bs_h([],Alt,Ok) when Ok == false ->
  bs_h(Alt,[],false);
bs_h(L,Alt,Ok) ->
  io:format("~p~n",[L]),
  if
    is_list(L) ->
     [H1 | L2] = L,
     if
      is_list(L2) ->
        [H2 | T] = L2,
        if
         H1 < H2 -> bs_h([H2 | T], [H1 | Alt], Ok);
          true    -> bs_h([H1 | T], [H2 | Alt], false)
         end;
        true -> bs_h([], [L2 | Alt], false)
      end;
   true -> bs_h([], [L | Alt], false)
  end.

%

fun_test() ->
  L1 = [4,9,1,3,-8,4,55,0,12,3,001,98,2],
  bs(L1).


In der Shell bekomme ich diese Anzeige und den folgenden Fehler:

Code:

30> sortieren:fun_test().
[4,9,1,3,-8,4,55,0,12,3,1,98,2]
[9,1,3,-8,4,55,0,12,3,1,98,2]
[9,3,-8,4,55,0,12,3,1,98,2]
[9,-8,4,55,0,12,3,1,98,2]
[9,4,55,0,12,3,1,98,2]
[9,55,0,12,3,1,98,2]
[55,0,12,3,1,98,2]
[55,12,3,1,98,2]
[55,3,1,98,2]
[55,1,98,2]
[55,98,2]
[98,2]
"b"
** exception error: no match of right hand side value []
     in function  sortieren:bs_h/3


Das "b" ist mir unerklärlich. Kann mir jemand helfen?

Vielen Dank!
Rainer
View user's profile Send private message
bpuzon
Posted: Tue Jun 08, 2010 9:51 am Reply with quote
User Joined: 05 Aug 2009 Posts: 23 Location: Cracow, Poland
Hallo, Rainer,

Erlang speichert Texte in der Listen mit Integers. Wenn eine List nur diese Elemente hat, und alle Elemente man wie ein Buchstabe interpretieren kann, dann zeigt Erlang diese List wie ein Text. "b" ist [98].

Der Fehler ist wahrscheinlich hier:
Code:

(...)
if
 is_list([98]) ->  %%Das ist OK
  [98 | []] = L    %% Das ist OK
  if
   is list([]) ->  %%[] IST eine List!
   [H2 | T] = []   %%Hier liegst der Feherl, [] hat diese Struktur nicht

_________________
Saludos,
Bartłomiej Puzoń
Erlang Solutions
View user's profile Send private message Visit poster's website
Rainer
Posted: Tue Jun 08, 2010 10:43 am Reply with quote
Joined: 08 Jun 2010 Posts: 2 Location: Saarbrücken
Hola Bartłomiej,

vielen Dank für das Anschauen und Deine Erklärung,
jetzt habe ich es verstanden.

Viele Grüsse
Rainer
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum