prolog code explanation - prolog

Hello could someone help me udnerstand how this code works ?
go(Start,Dest,Route):-
go0(Start,Dest,[],R),
rev(R,Route).
go0(X,X,[X|T]).
go0(Place,Y,T,R):-
legalNode(Place,T,Next).
go0(Next,Y,[Place|T],R)
legalNode(X,Trail,Y):-
(a(X,Y);a(Y,X)),
legal(Y,Trail).

I'm assuming this is taken from the "Programming in Prolog" book? It is actually pretty well explained in there.
What the code does is given a start location and a destination give you a route if it exists. This will be put in Route.
rev, as defined in the book, reverses a results stored in R and places it in Route, basically because the result is backwards.
The remainder of the code looks for a possible route from Start to Dest by checking if there is a direct link (that's what the a(X,Y) define) between two locations or if you can get there via one of the locations there is a direct link with.
With that and the book in hand, you should be able to figure out the code.

Related

How do you make a function for a n-pointed star? in mathematica

Im trying to make a function named star for an n-pointed star with radius 1, center (0,0) and start line is (0,1) with an angle n(((n-1)/n)*Pi). How??
My end goal is to make the star on the image I posted:
What mathematica functions do I use? graphics, Line, Table??
If possible id love an example :D.
I have not gotten far, the only code I have written is: star[n_] := Graphics[Table[Line[{}, {}], {}]];
Try
star[n_]:=Graphics[Line[Table[{Cos[t*(n-1)^2/n*Pi],Sin[t*(n-1)^2/n*Pi]},{t,0,n}]]];
star[5]
star[13]
Take it apart, look at what just the Table[...] gives you. And exactly why does that use {t,0,n} instead of {t,0,n-1}. And why does it have (n-1)^2/n in it, what does that do? Then look up the documentation for Line[...] and see what it accepts for input and how the output of Table[...] provides that.
Then the challenging part is that you have to reverse engineer my thought process to figure out what I was thinking and how I got to t*(n-1)^2/n*Pi. If you believe you understand all this then see if you can rewrite the calculations in a different way and still get the same result. And then can you find a way to make this much simpler and easier to understand exactly what it is doing and exactly why?

What's the point of the "/" after defining something in webfocus?

I'm currently learning webfocus and I'm learning about defining, but what is teaching me isn't doing a great job. An example that has been given to me is about cars. If I want to make a new field to find the difference between the retail cost and the dealer cost, they said to input
DEFINE FILE CAR
CALCULATED_DIFFERENCE/D5 = RETAIL_COST - DEALER_COST
END
What I'm confused about is what the
/D5
is there for. Is it required to define a file? Does it have to be something specific? I've researched a lot but haven't found any answers about it.
From what I understand, it joins the aliases and roots. For example the D could be short for difference. The 5 could just be a number. Without all of the example and question it's hard to say.
Here is another example http://www.hwconline.com/ibi_html/javaassist/intl/EN/help/source/topic547.htm
Section
A preceding forward slash '/' is required for all aliases and context roots. The WebFOCUS Administration Console automatically adds a forward slash if one was not entered.
I am assuming it could be a pointer. Look at the url and the / indicates like a tree node. If you want to get to a location then it's location/location/location/here
here being what your looking at. So if the /D5 is called it could mean page/page/page/Difference5 so for short it omits all of the pages and signifies the direct alias D5.
I am not entirely sure but that is what I have always thought. Look at everything dealing with locations a \ or / is always used.
I've found out what it means. The /D5 is known as formatting. It allows WebFOCUS to know what is being used in the file. The D stands for decimal, which means that if I wanted to I could format it as D/5.2 and an A stands for alphanumeric. If I were to define a file called person_last_name, I would use A/30. The number determines how much data it can hold, so 30 is all I need as a person's last name probably won't be longer than 30 characters.

Prolog : Comparing a range of times

Quick rundown, basically, the idea of the prolog program (GNU Prolog) is to search a database containing people with available time slots to a set of times (beginning time, end time) and return the first person who can meet in that time. The input has the syntax
meeting(Person,slot(time(10,0),time(12,30)))
I have a predicate which matches the above as such:
meeting(Person, slot(time(A,B),time(C,D))) :- %insert code
and the database entries look as such:
free(george,slot(time(9,30),time(11,0)))
Where I am stuck is that I'm not sure how I can compare the times in the database with the times entered when calling my meeting predicate. Not looking for a free answer, just wanting a push in the right direction and a good example :) Thanks everyone!
Doing what mbratch said, I saw better how prolog runs through the database and I was able to easily write come comparing logic which would satisfy the requirements.
The idea is, calling free(...) as above, Person receives the first individual in the list and all the passed variables receive the data. Then I could use my logic on the data and if all the logic passes, the method runs through and the proper response is returned.
Thanks for your help!

recaptcha still submits form when one word invalid

Um so I was in for a little bit of a surprise tonight. I spent a good 20 mins trying to figure out why I was able to submit a form knowing that what I entered into the recaptcha field was invalid. Is it true that you don't need to input the exact words it displays? If it shows me two words and I misspelled one of the words, I still pass validation? Same goes if "hello world" and I input "hell man" it still works.
With recaptcha, you are only tested on one of the words, while the other is used to help computers in scanning printed material. So you only need to get one right to pass (which one you need is random). :D
the recaptcha site describes why this is. You need to get one of the two words correct; only recaptcha knows which one.
But if a computer can't read such a
CAPTCHA, how does the system know the
correct answer to the puzzle? Here's
how: Each new word that cannot be read
correctly by OCR is given to a user in
conjunction with another word for
which the answer is already known. The
user is then asked to read both words.
If they solve the one for which the
answer is known, the system assumes
their answer is correct for the new
one. The system then gives the new
image to a number of other people to
determine, with higher confidence,
whether the original answer was
correct.

Detecting misspelled words

I have a list of airport names and my users have the possibility to enter one airport name to select it for futher processing.
How would you handle misspelled names and present a list of suggestions?
Look up Levenshtein distances to match a correct name against a given user input.
http://norvig.com/spell-correct.html
does something like levenshtein but, because he doesnt go all the way, its more efficient
Employ spell check in your code. The list of words should contain only correct spellings of airports.
This is not a great way to do this. You should either go for a control that provides auto complete option or a drop down as someone else suggested.
Use AJAX if your technology supports.
I know its not what you asked, but if this is an application where getting the right airport is important (e.g. booking tickets) then you might want to have a confirmation stage to make sure you have the right one. There have been cases of people getting tickets for the wrong Sydney, for instance.
It may be better to let the user select from the list of airport names instead of letting them type in their own. No mistakes can be made that way.
While it won't help right away, you could keep track of typos, and see which name they finally enter when a correct name is entered. That way you can track most common typos, and offer the best options.
Adding to Kevin's suggestion, it might be a best of both worlds if you use an input box with javascript autocomplete. such as jquery autocomplete
edit: danish beat me :(
There may be an existing spell-check library you can use. The code to do this sort of thing well is non-trivial. If you do want to write this yourself, you might want to look at dictionary trie's.
One method that may work is to just generate a huge list of possible error words and their corrections (here's an implementation in Python), which you could cache for greater performance.

Resources