Your description of what you intend to do is a correct solution, but is
not what the code you include does. I think perhaps you are still
thinking in procedural terms.
Quote:
> I'm still having problems with the tokeniser section. This is what I have so
> far:
> go(DateTimeString, L) :-
> name(DateTimeString, L), %Get the input string and use name/2 to
> turn into ascii codes
> space(L). %pass the ascii code list to
> getrest
> %getrest picks off the head.
> Pass it to space to see if it is a ' '.
I can see no calls to getrest in your code, presumably when you say
getrest you mean space/1.
Quote:
> space([46 | Rest]) :- !. %If the character is a space (46) then
> go to next char.
How exactly is this going to the next char? What you have written just
succeeds and does nothing else.
Quote:
> space(X | Rest) :-
Since you have the equivalent correct in the first clause I presume this
is a typo and you mean space([X | Rest])
Quote:
> append(X, [], Date), %If the char is not a space then store
> it in a new list.
> store = Date, %store the char in a variable
> called store
Firstly, variables in prolog begin with capitals or an underscore: store
is an atom, this will just fail unless it is a typo. Secondly, Date *is*
a variable that happens to be unified with a list conatining the single
element X; why exactly are you trying to store this in a new variable?
This looks to me as if you are still thinking in terms of procedural
language destructive assignment. Remember, in prolog there are no global
variables, and there is no destructive assignment, just unification. To
return something from a predicate you must it unify with a variable in
the head of the predicate.
Quote:
> space(Rest). %call space again
> My method has been to use recursion to pick the head off the list, check if
> it is a
> delimiter character (',' or ';' or ',' or ' ') then put the ascii codes into
> lists
> for each token inside a larger list.
> Sorry if this seems whole thing seems like a silly question, but I've only
> just started using Prolog. Once I've picked off a head and determined that
> it isn't a space, I store it in a list called Date using append. i then want
> to call space again recursively so that the list gets filled up.
How is this same list being filled up? You currently append to a new
list in each call.
Quote:
> The problem I have that I dont want to keep the delimiters - how do I skip
> them and get the next load of ascii codes into another list?
Your first clause of space/1 does skip something, or at least it ignores
something. It just needs to go on to get the rest of the ascii codes.
Quote:
> And how do I
> keep each list so that I can put them all into one larger list at the end?
> I've tried declaring variables to store each list, but I keep getting the
> error about singletons.
The warning about singletons just means that there is a variable in a
predicate that appears only once - I imagine that in this case it is the
Rest in the first clause of space/1. Normally a singleton means you are
not doing what you intended, although sometimes you may not care about
all the arguments of a predicate, and this singleton is intentional. In
the latter case you can avoid the annoying warning by prepending an
underscore to the variable name.
--
Andrew Eremin
IC Parc, William Penney Lab., Tel: +44 (0)20 7594 8299
Imperial College Fax: +44 (0)20 7594 8432