LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2010, 11:42 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
prolog problem


Hello i have a problem in prolog,i hope someone can help me,Im trying to swap the first 2 elements of a list:

Code:
swap([X,Y | Rest], [Y,X | Rest1]).
And when i do :
Code:
swap([1,2,3,4], What).
I get: What = [2,1|_3ac]
yes
 
Old 12-10-2010, 05:53 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, vbx_wx -

I turned this up from Google:
http://www.tek-tips.com/viewthread.c...1629112&page=1

Or this:
Code:
http://forums.devarticles.com/programming-tools-11/prolog-lists-1650.html

% swap first two elements of list
swap([X,Y|Rest], [Y,X|Rest1]).
The same link continues:
Code:
http://forums.devarticles.com/programming-tools-11/prolog-lists-1650.html

when tested it shows:
swap([1,2,3],SL).
SL = [2,1,3] ? ; % swaps first two
SL = [1,3,2] ? ; % swaps last two (first two of tail)
SL = [1,2,3]
yes

swap([1,2,3,4],SL).
SL = [2,1,3,4] ? ; % swaps first two
SL = [1,3,2,4] ? ; % swaps middle two (first two of tail)
SL = [1,2,4,3] ? ; % swaps last two (first two of new tail)
SL = [1,2,3,4]
yes

swap([1,2,3,4,5],SL).
SL = [2,1,3,4,5] ? ; % swaps first two
SL = [1,3,2,4,5] ? ; % swaps next two
SL = [1,2,4,3,5] ? ; % swaps next two
SL = [1,2,3,5,4] ? ; % swaps last two
SL = [1,2,3,4,5]
yes
Clocksin and Mellish is the best book available for learning Prolog:

This link also looks interesting:
http://ktiml.mff.cuni.cz/~bartak/prolog/index.html

'Hope that helps .. at least a little bit

Last edited by paulsm4; 12-10-2010 at 06:00 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Anyone know Prolog? Need halp steve296 Programming 10 07-21-2010 02:47 PM
Installation problem with Prolog manolakis Linux - Newbie 1 01-13-2007 01:11 AM
Prolog Help with if-then-else-else if?? magicvash Programming 1 05-27-2005 09:31 AM
Prolog problem glam1 Programming 0 10-10-2004 10:10 AM
Prolog Compiler mikeshn Programming 2 12-05-2003 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration