Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 29th August 2002, 11:49 PM   #1
martinus Thread Starter
Imperator Augustus
 
martinus's Avatar
 
Join Date: Jun 2001
Location: Holy Roman Empire
Posts: 2,642
Default Programming Puzzle #2

Code:
x(a).
x(b).
x(c).
y([]).
y([H|T]):-y(T),x(H).
Q1: what is it?

Q2: what happens after entering

Code:
?- y(X).
Q3: what happens if you switch the last two terms around, ie.

Code:
x(a).
x(b).
x(c).
y([]).
y([H|T]):-x(H),y(T).
martinus is offline   Reply With Quote

Join OCAU to remove this ad!
Old 30th August 2002, 12:18 AM   #2
hast
(Banned or Deleted)
 
hast's Avatar
 
Join Date: Sep 2001
Location: dots in the location field break it. note
Posts: 2,034
Default

looks like prolog
hast is offline   Reply With Quote
Old 30th August 2002, 1:14 PM   #3
Geo
Member
 
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
Default Re: Programming Puzzle #2

Quote:
Originally posted by martinus
Code:
x(a).
x(b).
x(c).
y([]).
y([H|T]):-y(T),x(H).
Q1: what is it?

Q2: what happens after entering

Code:
?- y(X).
1) Its prolog. It checks the elements in a list for conformity to the x rules.
2) It will evaluate every element in the "list" X for x(a), x(b) and x(c), making sure each element is either a, b or c. If one of the elements is not it return "False". It will check them in reverse order and there will be the length of X many calls to Y.

Quote:
Originally posted by martinus
Q3: what happens if you switch the last two terms around, ie.

Code:
x(a).
x(b).
x(c).
y([]).
y([H|T]):-x(H),y(T).
[/B]
3) It will check the first element in X, ensure that it is a, b or c before checking the rest of X.


NB: Its been a while since Ive done prolog so Im not sure if the terminology is right, or even the answer.
__________________
"That's the way good software gets designed. So if you pull out a piece it won't run" - Steve Ballmer
Geo is offline   Reply With Quote
Old 30th August 2002, 1:46 PM   #4
martinus Thread Starter
Imperator Augustus
 
martinus's Avatar
 
Join Date: Jun 2001
Location: Holy Roman Empire
Posts: 2,642
Default

Hey, you are all right, it is Prolog. Geo, you are sort of re-describing the code in English. Perhaps I should have asked more precisely: what is the output - in simple terms? Whilst logically equivalent, there is a definite difference between the two versions...
martinus is offline   Reply With Quote
Old 30th August 2002, 2:23 PM   #5
Geo
Member
 
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
Default

heh, I cant even remember what prolog has as output.

Isn't the main difference that the first one checks the list in reverse order and the second one checks it in 'forwards' order?
__________________
"That's the way good software gets designed. So if you pull out a piece it won't run" - Steve Ballmer
Geo is offline   Reply With Quote
Old 30th August 2002, 3:18 PM   #6
Nasher
Member
 
Join Date: Jun 2001
Location: Hobart
Posts: 1,242
Default

Prolog is confusing, though not nearly as confusing as other similar languages.

J, anyone?
Nasher is offline   Reply With Quote
Old 31st August 2002, 11:09 PM   #7
martinus Thread Starter
Imperator Augustus
 
martinus's Avatar
 
Join Date: Jun 2001
Location: Holy Roman Empire
Posts: 2,642
Default

OK then. I will describe the output only. That way there should be an incentive left for those who are willing to learn more about the semantics of prolog and the backtracking mechanism.

In fact, prolog is usually explained using two different semantics. In the logical semantic, the comma operator represent the logical "and", which makes both programs above equivalent. In everyday's language, the programs say:

"The letters (atoms) a, b and c have the property x"
"The empty list has the property y"
"A list has property y, if the first element (the head) has property x, and the remaining list (the tail) has propert y.

the query says: "does the variable X have property y?"

prolog will now go and find "solutions" for X that satisfy the query.

Obviously in a logical sense all lists (all finite strings) containing a,b and c fulfill the query.

Indeed the first program produces:

Code:
X = [] ;

X = [a] ;

X = [b] ;

X = [c] ;

X = [a, a] ;

X = [b, a] ;

X = [c, a] ;

X = [a, b] ;

X = [b, b] ;

X = [c, b] ;

X = [a, c] ;

X = [b, c] ;

X = [c, c] ;

X = [a, a, a] ;

X = [b, a, a]
Here the output of the second program:

Code:
X = [] ;

X = [a] ;

X = [a, a] ;

X = [a, a, a] ;

X = [a, a, a, a] ;

X = [a, a, a, a, a] ;

X = [a, a, a, a, a, a] ;

X = [a, a, a, a, a, a, a] ;

X = [a, a, a, a, a, a, a, a] ;

X = [a, a, a, a, a, a, a, a, a] ;
Btw, if you want to try it yourself, a free and highly reputed prolog interpreter/compiler is available at http://www.swi-prolog.org .

Right, no winner for the first price this time... I'll keep the slab.
martinus is offline   Reply With Quote
Old 2nd September 2002, 10:03 PM   #8
DRILL SGT
Member
 
Join Date: Feb 2002
Location: Brisbane
Posts: 511
Default

What is the user of ProLog, is it an Array driven language or something. I being a 16 yo I have never head of it.
DRILL SGT is offline   Reply With Quote
Old 3rd September 2002, 10:16 AM   #9
martinus Thread Starter
Imperator Augustus
 
martinus's Avatar
 
Join Date: Jun 2001
Location: Holy Roman Empire
Posts: 2,642
Default

prolog stands for programming in logic. essentially you describe the problem and the prolog interpreter finds the solution(s) for you. there's more info on google.
martinus is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 4:54 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!