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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 9th, 2008, 10:23 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 - PROLOG Problem

First of all let me apologize for posting in the wrong forum ... but I didn't find one for prolog... please redirect me if we have one ... and anyone's response if he/she knows the answer is appreciated.

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 9th, 2008, 10:36 AM
Thr3ddy's Avatar
Thr3ddy Thr3ddy is offline
Harbinger of Harbingers
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Coral Springs, Florida, USA
Posts: 876 Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)Thr3ddy User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 43981 Folding Title: Beginner FolderFolding Points: 43981 Folding Title: Beginner FolderFolding Points: 43981 Folding Title: Beginner Folder
Time spent in forums: 1 Week 6 Days 46 m 24 sec
Reputation Power: 251
Did you look at the Other Programming Languages forum? Move requested.
__________________
Regards,
Eddy Luten.

Information: C, C++, STL, Boost, OpenMP, Scriptionary, Google
Book of the moment: Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides

This post and all subsequent posts by "Thr3ddy" are licensed under the Creative Commons Attribution United States License 3.0: attribute "Eddy Luten" for any code used which was extracted from "Thr3ddy's" posts.

Reply With Quote
  #3  
Old April 14th, 2008, 07:13 AM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
Thread moved from PHP to Other Programming Languages ...
__________________
# Jeremy

Explain your problem instead of asking how to do what you decided was the solution.

Reply With Quote
  #4  
Old April 24th, 2008, 03:54 PM
yipyip yipyip is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: Germany
Posts: 4 yipyip User rank is Sergeant (500 - 2000 Reputation Level)yipyip User rank is Sergeant (500 - 2000 Reputation Level)yipyip User rank is Sergeant (500 - 2000 Reputation Level)yipyip User rank is Sergeant (500 - 2000 Reputation Level)yipyip User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 26 m 14 sec
Reputation Power: 0
...nobody answers here?
So i do.

The key idea is:
You need 2 clauses.

Try something like

Code:
display_list([]) :-
    write('The list is empty'). 
        
display_list(L) :-
    displs(1, L).


displs(_, []).

displs(COUNTER, [X|REST]) :-
 "your code here, with incrementing the counter
  and calling this clause with REST"


Forget the cut and your increment clause.
I hope this helps a little.
Comments on this post
jharnois agrees: Welcome to Dev Shed!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Other - 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