% $Id: read-integer.pl,v 1.1 2014/09/18 11:21:53 machan Exp $
% 元ネタ:【論理】Prolog【初心者】
% http://p...content-available-to-author-only...h.net/test/read.cgi/tech/1289016056/751-756
read_integers(Xs) :- read_terms(Ts), select_integer(Ts, Xs).
read_terms
(Xs
) :- read(X
), read_terms_1
(X
, Xs
).
read_terms_1(X, []) :- X = end_of_file.
read_terms_1
(X
, [X
|Xs
]) :- X \
= end_of_file
, read(Y
), read_terms_1
(Y
, Xs
).
select_integer([], []).
select_integer
([X
|Xs
], [X
|Ys
]) :- integer(X
), select_integer
(Xs
, Ys
).select_integer
([X
|Xs
], Ys
) :- \+integer(X
), select_integer
(Xs
, Ys
).
%%%%%%%% Example %%%%%%%%
%
% ?- read_integers(L).
% |: 1.
% |: 2.
% |: a.
% |: 3.
% |: (4, 5).
% |: 6.
% |: "7".
% |: 8.
% |: L = [1, 2, 3, 6, 8] ;
% false.
%
% ?-
JSAkSWQ6IHJlYWQtaW50ZWdlci5wbCx2IDEuMSAyMDE0LzA5LzE4IDExOjIxOjUzIG1hY2hhbiBFeHAgJAolIOWFg+ODjeOCv++8muOAkOirlueQhuOAkVByb2xvZ+OAkOWIneW/g+iAheOAkQolICAgIGh0dHA6Ly9wLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5oLm5ldC90ZXN0L3JlYWQuY2dpL3RlY2gvMTI4OTAxNjA1Ni83NTEtNzU2CgoKcmVhZF9pbnRlZ2VycyhYcykgOi0gcmVhZF90ZXJtcyhUcyksIHNlbGVjdF9pbnRlZ2VyKFRzLCBYcykuCgoKcmVhZF90ZXJtcyhYcykgOi0gcmVhZChYKSwgcmVhZF90ZXJtc18xKFgsIFhzKS4KCnJlYWRfdGVybXNfMShYLCBbXSkgICAgIDotIFggPSAgZW5kX29mX2ZpbGUuCnJlYWRfdGVybXNfMShYLCBbWHxYc10pIDotIFggXD0gZW5kX29mX2ZpbGUsIHJlYWQoWSksIHJlYWRfdGVybXNfMShZLC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