%% ---
%%  Excerpted from "Programming Erlang",
%%  published by The Pragmatic Bookshelf.
%%  Copyrights apply to this code. It may not be used to create training material,
%%  courses, books, articles, and the like. Contact us if you are in doubt.
%%  We make no guarantees that this code is fit for any purpose.
%%  Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
%%  
%%  日本語版の『プログラミングErlang』、およびサンプルコードについては、
%%   http://ssl.ohmsha.co.jp/cgi-bin/menu.cgi?ISBN=978-4-274-06714-3 を確認してください。
%% ---
-module(lists1).
-compile(export_all).

%% 注：このモジュールをコンパイルするのは
%% 構文チェックのためだけ
%% BEAMコードは作らない
%% START:map
map(_, [])     -> [];
map(F,  [H|T]) -> [F(H)|map(F, T)].
%% END:map


member(H, [H|_]) -> true;
member(H, [_|T]) -> member(H, T);
member(_, [])    -> false.

    

partition(Pred, L) -> partition(Pred, L, [], []).

partition(Pred, [H|T], Ts, Fs) ->
    case Pred(H) of
	true  -> partition(Pred, T, [H|Ts], Fs);
	false -> partition(Pred, T, Ts, [H|Fs])
    end;
partition(_, [], Ts, Fs) ->
    {reverse(Ts), reverse(Fs)}.



reverse(L) -> reverse(L, []).

reverse([H|T], L) -> reverse(T, [H|L]);
reverse([], L)    -> L.



search(Key, [{Key,Val}|_]) -> {ok, Val};        %% <callout id="lists1.1"/>
search(Key, [_|T])         -> search(Key, T);    %% <callout id="lists1.2"/>
search(_, [])              -> error.             %% <callout id="lists1.3"/>

    
