How can I call a function random inside other function in prolog? - prolog

I'm trying to call the random function inside another function. For example I want to do this assert(fact(random()). But it does not work. How can I insert a random number this way? Thanks.

In prolog there is no concept of functions like you are trying to do in your code. You should do:
random(N), assert(fact(N))
I recommend reading at least first two chapters Learn Prolog Now! to better understand search and unification.

Related

What is the difference between write and respond in prolog

Based on the code I've got, there is no explicit difference between those two procedures. I can use both of them to print things I want to.

Prolog beginner help get current time

I am modifying the Eliza program http://cs.nyu.edu/courses/fall11/CSCI-GA.2110-003/documents/eliza.pl
trying to get it to print the system time when user asks - What Time is it?
After hours of reading through manual I got my get_date_time_value() function to work.
As in
get_date_time_value(Key, Value) :-
get_time(Stamp),
stamp_date_time(Stamp, DateTime, local),
date_time_value(Key, DateTime, Value).
However I am at a loss as to how do I call this function from my rule which is defined as
rules([[time,0],[
[1,[_],0,
[please,do,not,get_date_time_value(time,x),.],
['I',dont,know,the,time,.]]]]).
Yes this is a homework assignment and this might sound silly to experts ,but I am really new to Prolog programming even though I have quite some experience in object oriented and functional programming.
No matter what parameters I pass to the get_date_time_value(time,X) function I am always getting an error.
I spent all night on a hit an trial approach ,but nothing I do works.
Any pointers will be great!!
Thanks
From the structure I guess it should look something like this:
rules([[time,0],[
[1,[_],0,
[it,is,HourLiteral,oclock,.],
['I',dont,know,the,time,.]]]]) :- get_date_time_value(hour, HourNumber), number_codes(HourNumber, HourString), atom_string(HourLiteral, HourString) .
I do not know if it works. I did not test it.
You do not give any idea of what you mean by your rule. Maybe you are trying to have the current time in the list where the term get_date_time_value(time,x) appears: but, is that term a call to a function? Prolog does not support that: just look at the clause you give for the get_date_time_value/2 predicate (not function) and what you see there is a sequence of calls to predicates. So your rule probably must given in a clause that holds only if the call to your get_date_time_value/2 predicate also holds, and the clause head and the call share variable(s) to pass information between them.

How can I reuse a Prolog data structure?

I am writing a small program using Prolog. There is a data structure that I want to reuse, so I tried assigning it to a variable:
CitizenProfile = voter_profile(citizen,not_in_prison).
Then I used it like this:
state(alabama, [CitizenProfile]).
However, I am encountering this error when I compile my file from the console:
**[No permission to modify static_procedure `(=)/2'][1]**
==
I even tried declaring the equal sign dynamic, but that didn't solve anything. :(
:- dynamic (=)/2.
The reason for the error is that it looks to Prolog like you're trying to do this:
=(CitizenProfile, voter_profile(citizen,not_in_prison)).
This looks just like any other fact definition. =/2 could just as easily be foobar/2:
foobar(CitizenProfile, voter_profile(citizen,not_in_prison)).
Now, if we were in the middle of some rule body, this might be a legitimate way to establish a variable binding. Then everything would be culminating in this:
foo :- ...,
CitizenProfile = voter_profile(citizen,not_in_prison),
state(alabama, [CitizenProfile]).
That would be the same as saying this:
foo :- ...,
state(alabama, [voter_profile(citizen,not_in_prison)]).
If this expansion is what you're trying to accomplish, there is unfortunately no way to create shorthand in a fact database like this. You could, as #hardmath says, use assertz/1 to accomplish it, which would look like this:
make_database :-
CitizenProfile = voter_profile(citizen,not_in_prison),
assertz(state(alabama, [CitizenProfile])).
This would be kind of sketchy behavior though, because you're putting static information into the dynamic store. In my experience, one doesn't usually want to build up large structures in the database. It's usually cleaner and easier to build several relations and "join" across them in a relational manner. I'm not sure what all you're going to want here, so this is just a sketch, but this is kind of what I'd expect to see:
voter_profile(voter1, alabama, citizen, not_in_prison).
voter_profile(voter2, alabama, citizen, in_prison).
voter_profile(voter3, new_mexico, citizen, not_in_prison).
rather than what I presume you'd be building (eventually), which I picture more like this:
state(alabama, [voter_profile(citizen,not_in_prison), voter_profile(citizen, in_prison)]).
state(new_mexico,[voter_profile(citizen,not_in_prison)]).
The temptation to create a bunch of lists is understandable, but Prolog's database can't really help you with processing them. You'll wind up resorting to a lot of member/2 and O(N) searching which will add up to pretty bad performance. By default, Prolog will index on the first argument, but each implementation defines some indexing declarations you can use to make it index the second or Nth arguments in addition or instead. You can then use bagof/3 or findall/3 to reconstitute the lists if you need all the results.
Probably what you want is to define a dynamic predicate voter_profile/2 and assertz new facts "dynamically" to be remembered by that predicate store (the clause database). I say "probably" because you haven't made it clear how a state (e.g. Alabama) should be related to a particular citizen profile.
See here for the SWI-Prolog builtin assertz/1 documentation and much more on database mechanisms of SWI-Prolog.

What's the best way to name a predicate function?

I'm looking at a function called:
WhetherAddFloor(leg) -> bool
and when I see code like:
if(WhetherAddFloor(l)) ...
it smells odd and am wonder if something like:
CheckAddFloorNeeded(leg) -> bool
wouldn't be better? Thoughts?
In languages where question marks are allowed in function names the style is to append one to the end if the function returns boolean:
FloorNeeded?(leg)
But where that isn't syntactically possible the convention is to use the word 'is' at the beginning:
isFloorNeeded(leg)
It's generally easy to read and understand and it seems like it'll fit your situation.
WhetherAddFloor() sounds as quite an unfortunate choice to me.
CheckAddFloorNeeded() is better, if the function has a substantial amount of work to do, and you would like to advertise that fact to those who use the interface.
IsFloorNeeded() is also good, if the function does not have a substantial amount of work to do, (say, it is just an accessor to a previously computed member,) or if you want to keep this little bit of information secret from those using the interface.

Natural sort algorithm implementation woes

I have Googled ASP Classic implementations of the natural sort algorithm to no avail. Best I was able to find was from Dave Koelle, which was from a question on SO. Issue is that since I need to implement this algorithm in ASP Classic, I don't have access to certain functions such as
Collections.sort(your list, new AlphanumComparator());
Ideally, I'd like to pass an array to a function and have it return to me the ordered array.
Any ideas as to what I could do?
Thank you.
You haven't specified which language you are using in ASP. Typically this would be VBScript.
However if you were to use JScript instead then you can use JScript's array object and use its sort method. This method optionally takes as a parameter a comparator function.
var a = new Array();
// code to populate array
a.sort(function() { // Comparator code returning (-1|0|1) });
There is no need to convert everything to JScript, you can use utilities written in JScript from VBScript.

Resources