Help!

Ulf Wiger (ÄL2/EAB) <>
Thu Aug 14 09:44:35 CEST 2003


From: Vladimir Sekissov [mailto:]

>f(X) ->
>  ...
>  {_, Chapters} = element(4, Book),
>  case lists:keysearch(X, 2, Chapters) of
>    {value, {_, _, Header}} ->
>      {ok, Header};
>    Other ->
>      Other
>  end.
>
>But may be it would be better to use records here if 
>book format is fixed:
>
>-record(book, {preface, contents, chapters}).
>-record(chapter, {num, header}).
>
>get_chapter(N, B=#book{chapters=Cs}) ->
>  case lists:dropwhile(fun (C=#chapter{num=N}) -> false;
>                           (_) -> true
>                       end, Cs) of
>    [C=#chapter{header=H}|_] ->
>      {ok, Header};
>    [] ->
>      undefined
>  end.

Just for the record, you can use record syntax in 
lists:keysearch/3 as well:

case lists:keysearch(X, #chapter.num, Chapters) of ...


/Uffe



More information about the erlang-questions mailing list