-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfig6_6.pl
40 lines (25 loc) · 1.03 KB
/
fig6_6.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
% Figure 6.6 A procedure to transform a sentence into a list of atoms.
/*
Procedure getsentence reads in a sentence and combines the words
into a list of atoms. For example
getsentence( Wordlist)
produces
Wordlist = [ 'Mary', was, pleased, to, see, the, robot, fail]
if the input sentence is:
Mary was pleased to see the robot fail.
*/
getsentence( Wordlist) :-
get0( Char),
getrest( Char, Wordlist).
getrest( 46, [] ) :- !. % End of sentence: 46 = ASCII for '.'
getrest( 32, Wordlist) :- !, % 32 = ASCII for blank
getsentence( Wordlist). % Skip the blank
getrest( Letter, [Word | Wordlist] ) :-
getletters( Letter, Letters, Nextchar), % Read letters of current word
name( Word, Letters),
getrest( Nextchar, Wordlist).
getletters( 46, [], 46) :- !. % End of word: 46 = full stop
getletters( 32, [], 32) :- !. % End of word: 32 = blank
getletters( Let, [Let | Letters], Nextchar) :-
get0( Char),
getletters( Char, Letters, Nextchar).