Exercises Tutorial 4
read_int( [ a, b, 1, 2, [ 3, 4], 5 ] ).
1
2
5
This first procedure will look something like this…
read_int( [ ] ). % Stop when the list is empty
% If the element is an integer print it…
read_int( [ H | T ] ) :-
integer( H ),
nl, write( H ),
read_int( T ).
% Otherwise move onto the next element
read_int( [ H | T ] ) :-
read_int( T ).
read_int( [ a, b, 1, 2, [3, 4], 5 ] ).
1
2
3
4
5
The second procedure will look something like this…
read_int2( [ ] ).
read_int2( [ H | T ] ) :-
integer( H ).
nl, write( H ),
read_int2( T ).
read_int2( [ H | T ] ) :-
is_list( H ),
read_int2( H ),
read_int2( T ).
read_int2( [ H | T ] ) :-
read_int2( T ).