Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old April 9th, 2008, 10:45 AM
metahuman metahuman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 9 metahuman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 51 sec
Reputation Power: 0
Other Language - PROLOG Problem

am trying to print the elements of a list.... one per line using a predicate of arity 1 .... which I am able to do with the code:

Code:
display_list([]) :- !, write('\nThe list is empty').		  % handles empty list
display_list([H|T]) :- write('        '), write(H), nl, display_list


The problem is: I want to have the sequence number of the list element in front of it: For example,

1 North
2 South
3 East
4 West

I am not able to able to do that.

The code I am writing is:

Code:
increment(X,Y) :- Y is X+1.

display_list([])    :- !, write('\nThe list is empty').		  
display_list([H|T]) :- X = 0, increment(X,Y), write(Y),write('        '), write(H), nl, display_list(T).increment(X,Y) :- Y is X+1.


The output I get as expected is:
11 ?- display_list([north, south, east]).
1 north
1 south
1 east

The list is empty
true.

Also I keep on getting this 'The list is empty' everytime the elements are exhausted and not only when the input list is empty .....

Please advice ...... Thanks

Reply With Quote
  #2  
Old April 11th, 2008, 04:40 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,070 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 39 m 40 sec
Reputation Power: 443
I've never really worked with Prolog before, so I may be off here (especially regarding where to put the cut), but from what I read here I think you need a rule that matches a one-element list between the empty list match and the match for a mist of more than one element:

Code:
display_list([H]) :-  X = 0, increment(X,Y), write(Y),write('        '), write(H), nl, !
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Reply With Quote
  #3  
Old June 1st, 2008, 09:58 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Unless there's a particular reason that you're limiting yourself to a single predicate then try this:

Code:
display_list(List) :-  display_list(List,1).

display_list([Head|Tail],Count) :-
  Number is Count + 1,
  write(Count), write(' '), write(Head), nl,
  display_list(Tail,Number).


Providing a simplified interface to a predicate like this is a very common idiom in Prolog. It also allows access to the raw predicate should you need it. The output is as expected.

Code:
?- display_list(['North','South','East','West']).
1 North
2 South
3 East
4 West
fail.

?- display_list(['North','South','East','West'], 0).
0 North
1 South
2 East
3 West
fail.

?- 


I hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Other Language - PROLOG Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway