Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/12/84; site nbs-amrf.UUCP Path: utzoo!watmath!clyde!bonnie!akgua!gatech!seismo!lll-crg!gymble!umcp-cs!nbs-amrf!hopp From: hopp@nbs-amrf.UUCP (Ted Hopp) Newsgroups: net.lang.prolog Subject: Re: "name" and type conversion Message-ID: <77@nbs-amrf.UUCP> Date: Sun, 24-Nov-85 15:37:41 EST Article-I.D.: nbs-amrf.77 Posted: Sun Nov 24 15:37:41 1985 Date-Received: Mon, 25-Nov-85 08:07:58 EST References: <382@bcsaic.UUCP> Organization: National Bureau of Standards Lines: 48 > ... one of our programmers discovered that the following succeeds: > ...name ('12', X), name (Y, X), integer (Y)... > In other words, running such an atom into and then back out of "name" > converts it from an atom to an integer! > > Now this is a very handy thing to have. However, I suspect that it's > not supposed to work that way, and probably doesn't in other Prologs. > Any comments? Does anyone have another (simple) way of doing such a > conversion? > -- > Mike Maxwell > Boeing Artificial Intelligence Center > ...uw-beaver!uw-june!bcsaic!michaelm This works in C-Prolog v 1.3 as well. According to the C-Prolog manual, name(X,L) succeeds when L is a list of ASCII codes of the characters for X, not only when X is an atom, but when X is an integer. Thus, name('12',L), name(12,L). succeeds. C-Prolog (and, I suspect, DEC-10 Prolog, on which C-Prolog is based) differ in this from Clocksin and Mellish, where the first argument to name/2 must be an atom. One way of evaluating a string of ASCII digits as an integer is as follows: int(N,[],N) . int(N,[X|L],M) :- ascii_digit(X), M1 is 10*N + X - 48, int(M1,L,M) . ascii_digit(X) :- 48 =< X, X =< 57 . A sample call is: name('123',L), int(0,L,M) which binds M to 123. If int(0,L,M) fails, L is not a list of digits, or M cannot be unified to the evaluation of L (e.g., M was already unified to a different integer value). (If you can assume that the second argument will always be a list of digits, the call to ascii_digit/1 can be omitted from the second clause of int/3.) Prologs that define name/2 in strict accordance with Clocksin and Mellish, I suspect, will not be able to use the name-in/name-out trick. Use of a predicate such as int/3 above should always be safe. -- Ted Hopp {seismo,umcp-cs}!nbs-amrf!hopp