Discussions related to Visual Prolog
kimsmith64
Posts: 4
Joined: 24 Nov 2021 14:26

Working with Integers

Unread post by kimsmith64 »

Why is it more difficult to work with integers than strings.
I am trying to take an integer and put it into a file to then be used later in my program.
In the Personal addition I can't write an integer to a file, I have tried the myriad of options that I have discovered through material.
My current partial solution is to create a string and save in a file. I can then open the file and read the string and using toTerm create integers. Great!
What I actually need to do is save each element in the integer list to a different variable InAttribute1.. I can't use count as it gets set to 1 each time the process is called and an internally will get reset every time the procedure is called which does not allow me control.
If I could save as an integer and open and store as an integer this would be a lot easier.

Code: Select all

 foreach S in StrList do        processStrList(S, Count)  end foreach,   class predicates     processStrList : (string Data, integer Count). clauses     processStrList(ABC, Count) :-         IntA = toTerm(integer, ABC),         if Count = 1 then             InAttribute = IntA,             stdio::write("Attribute1 is ", InAttribute)         end if,         fail.     processStrList(_I, _S).
The other issue is when trying to access an element in the integer list I get a sytnax error.

Code: Select all

        Attribute1 = StrList[1],
This just gives me a 150 syntax error

Does anyone have any suggestions on how to achieve placing each integer into a different variable
Martin Meyer
VIP Member
Posts: 328
Joined: 14 Nov 2002 0:01

Re: Working with Integers

Unread post by Martin Meyer »

Hi Kim,

working with integers is same easy as with strings. Here are two working variants of your code which may hopefully help:

Code: Select all

clauses     run() :-         StrList = ["Huey", "Dewey", "Louie"],         processStrList(StrList, 1).   class predicates     processStrList : (string* DataList, integer Count). clauses     processStrList([Data | RestDataList], Count) :-         if Count = 1 then             stdio::write("The first element is ", Data)         else             stdio::write("A further element is ", Data)         end if,         stdio::nl(),         processStrList(RestDataList, Count + 1).       processStrList([], _Count).

Code: Select all

clauses     run() :-         StrList = ["-3", "0", "4"],         foreach Str in StrList do             Int = toTerm(Str),             processInt(Int)         end foreach.   class facts     count : integer := 1.   class predicates     processInt : (integer Data). clauses     processInt(Data) :-         if count = 1 then             stdio::write("The first element is ", Data)         else             stdio::write("A further element is ", Data)         end if,         stdio::nl(),         count := count + 1.
A syntax like Attribute1 = StrList[1] to get the first element of a list does not exist in VIP.
To unify Attribute1 with the head element of list StrList use:

Code: Select all

[Attribute1 | _RestStrList] == StrList
In detail that is explained in Lists and Recursion.
Regards Martin
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: Working with Integers

Unread post by Harrison Pratt »

Take a look at the list class for lots of useful predicates.

For example, you can retrieve the "nth" element of a list this way:

Code: Select all

        AttributeList = [1, 2, 3, 4, 5],         Attribute1 = list::nth(0, II), % <== note that list indices are 0-based         % returns Attribute1 = 1
If you are at all uncertain about the list size, you can use

Code: Select all

        Attribute1 = list::tryGetNth(0, AttributeList)
This will FAIL instead of causing a program exception, unlike list::nth(0,AttributeList)
kimsmith64
Posts: 4
Joined: 24 Nov 2021 14:26

Re: Working with Integers

Unread post by kimsmith64 »

Thank you so much for the replies.

I have got my code working. Much appreciated.
Post Reply