Prolog - sending a list as a parameter to be displayed - list

I'm trying to write a program to find a route between towns, add the path to the list and then, ad the end display it.
I think adding to the list works, but I'm having the problem displaying the list, don't know how can I pass a list as a parameter to be used when it's done finding the path? Hope you guys can help. Here's the code:
connected(middlesbrough, stockton).
connected(middlesbrough, darlington).
connected(stockton, sunderland).
connected(darlington, thirsk).
connected(stockton, newcastle).
connected(newcastle, york).
connected(thirsk, york).
connected(york, leeds).
connected(leeds, huddersfield).
connected(leeds, dewsbury).
connected(huddersfield, manchester).
connected(dewsbury, manchester).
run(List):-
write('Enter Starting City :'),
read(Start),
write('Enter Finishing City :'),
read(End),
findroute(Start,End),
writeList([List]).
findroute(Start,End):-
connected(Start,End).
findroute(Start,End):-
add(Start, List, [Start | List]),
connected(Start,Link), findroute(Link,End).
add(A,B,[A|B]).
writeList([]).
writeList([Head | Tail]):-
write(Head),
nl,
writeList(Tail).

Your findroute/2 predicate does not return the list, so the output can't work.
The call should look something like this:findroute(Start,End,List)
Obviously, the findroute/2 predicate must be changed to findroute/3:
findroute(Start,End,[Start,End]):-
connected(Start,End).
findroute(Start,End,List):-
connected(Start,Link),
add(Start,Rest,List),
findroute(Link,End,Rest).
(hint: be sure you understand why the add/3 call works even though Rest is uninstantiated at that point. Otherwise your tutor won't believe that this code is your homework! ;-) )
You may want to add a cut at the end of the first clause if you only want to find the shortest route.
Finally, List is already a list, so don't put square brackets around it when calling writeList/1!

Related

print values from list inside a list

i have this code in python:
sensor16=['1','-','\\','/','*','!']
sensor15=['4','G','H','I']
sensor14=['7','P','Q','R','S']
sensor13=['*']
sensor12=['2','A','B','C']
sensor11=['5','J','K','L']
sensor10=['8','T','U','V']
sensor09=['0',' ']
sensor08=['3','D','E','F']
sensor07=['6','M','N','O']
sensor06=['9','W','X','Y','Z']
sensor05=['#']
sensor04=['BACKSPACE']
sensor03=['DELETE ALL']
sensor02=['READ']
sensor01=['TRANSMITE']
sensor= [sensor01,sensor02,sensor03,sensor04,sensor05,sensor06,sensor07,sensor08,sensor09,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16]
press=[1,1,1,1,1,5,4,4,2,4,4,4,1,5,4]
num_press=0
for steps in range(15) :
sensor[steps]
num_press=press[steps]
for i in range(num_press) :
print(sensor[steps][num_press])
How can I access the value in each sensorXX list which corresponds to the value in press[] list?
For example press[9] is 4, so I want to print sensor10[4] which is V
the reason that i have to go through press[] list is that i have already managed to get some timing functions so i know how much time passed since my last press, so i can either printout the next character inside the specific sensor number list ( e.g sensor01[] or sensor[12] ) and when i reach the maximum number of presses to reloop or i have to move my cursor one place right and start from begin.
i have already build and running in arduino but the code is in C. now i would like to move everything to my raspberry pi 2 and in python.
this was where the first idea came from, and i actually used most of that code to do so in arduino.
youtube video for arduino use of my code
arduino code
I ran your code and got index out of range error. It looks like you just have off by one error. Try this: (Also removed your nested for loop it seems like a mistake)
for steps in range(15) :
sensor[steps]
num_press=press[steps]
print (sensor[steps]) [num_press-1]
Output:
TRANSMITE
READ
DELETE ALL
BACKSPACE
#
Z
O
F
V
L
C
*
S
I
DOOOOONE ! AND THANK YOU ALL FOR YOUR RESPONSE ! :D
sensor16=['1','-','\\','/','*','!']
sensor15=['4','G','H','I']
sensor14=['7','P','Q','R','S']
sensor13=['*']
sensor12=['2','A','B','C']
sensor11=['5','J','K','L']
sensor10=['8','T','U','V']
sensor09=['0',' ']
sensor08=['3','D','E','F']
sensor07=['6','M','N','O']
sensor06=['9','W','X','Y','Z']
sensor05=['#']
sensor04=['BACKSPACE']
sensor03=['DELETE ALL']
sensor02=['READ']
sensor01=['TRANSMITE']
sensor=[sensor01,sensor02,sensor03,sensor04,sensor05,sensor06,sensor07,sensor08,sensor09,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16]
press=[1,1,1,1,1,5,4,4,2,4,4,4,1,5,4]
num_press=0
steps=0
for steps in range (15):
for i in range (press[steps]):
print (sensor[steps][i])
and it goes to every value individually.
again ! thanks a lot ....

How to get every combination of a combolist

I have a program where you can import a combolist, ex.
test:lol
hello:hi
sup:hey
The first one being the "username" and last one being the "password".
I want so that every single username will have a line with each password of the list. I would like all of this put into a separate string list.
Such as:
test:lol
test:hi
test:hey
hello:lol
hello:hi
hello:hey
sup:lol
sup:hi
sup:hey
I think this might be a really simple code, something to do with first separating the list into two lists, then for each username, add every password, but my brains can't really think of anything to come up with a solution for this.
thank you in advance :)
Yes, you're right.
Firs - get two lists users and passwords by separating each string by column.
Second traverse users and passwords and combine them. In pseudocode
foreach uName in users do
foreach pwd in passwords do
print user.concat(":").concat(pwd)
end
end
Explanation:
Get first `uName` from `users`
Get first `pwd` from `passwords`
output "`user`:`pwd`"
Get second `pwd` from `passwords`
output "`user`:`pwd`"
...
Get second `uName` from `users`
Get first `pwd` from `passwords`
output "`user`:`pwd`"
...
Feel free to ask, if something left unclear

Creating a list from user input with swi-prolog

This is my first experience with Prolog. I am at the beginning stages of writing a program that will take input from the user (symptoms) and use that information to diagnose a disease. My initial thought was to create lists with the disease name at the head of the list and the symptoms in the tail. Then prompt the user for their symptoms and create a list with the user input. Then compare the list to see if the tails match. If the tails match then the head of the list I created would be the diagnosis. To start I scaled the program down to just three diseases which only have a few symptoms. Before I start comparing I need to build the tail of list with values read from the user but I can't seem to get the syntax correct.
This is what I have so far:
disease([flu,fever,chills,nausea]).
disease([cold,cough,runny-nose,sore-throat]).
disease([hungover,head-ache,nausea,fatigue]).
getSymptoms :-
write('enter symptoms'),nl,
read(Symptom),
New_Symptom = [Symptom],
append ([],[New_symptom],[[]|New_symptom]),
write('are their more symptoms? y or n '),
read('Answer'),
Answer =:= y
-> getSymptoms
; write([[]|New_symptom]).
The error occurs on the append line. Syntax Error: Operator Expected.
Any help with this error or the design of the program in general would be greatly appreciated.
This is one way to read a list of symptoms in:
getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]).
You type stop. when you want to finish the list.
You would then need to decide what logic you want to match the way you have represented a disease.
A complete example:
:-dynamic symptom/1.
diagnose(Disease):-
retractall(symptom(_)),
getSymptoms(List),
forall(member(X,List),assertz(symptom(X))),
disease(Disease).
getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]).
disease(flue):-
symptom(fever),
symptom(chills),
symptom(nausea).
disease(cold):-
symptom(cough),
symptom(runny_nose),
symptom(sore_throat).
disease(hungover):-
symptom(head_ache),
symptom(nausea),
symptom(fatigue).
create(L1):-read(Elem),create(Elem,L1).
create(-1,[]):-!.
create(Elem,[Elem|T]):-read(Nextel),create(Nextel,T).
go:- write('Creating a list'),nl,
write('Enter -1 to stop'),nl,
create(L),
write('List is:'),
write(L).

Make list from list of lists with condition

I'm currently stucked with recursion over lists of lists.
The task is quite simple in any imperative language: iterate over every professor, iterate over professor's course list, and get every course that matches CourseNumber given as predicate argument to output var CourseList.
I have simple solution (other ones just fail with out of stack or returns empty list), but as you can see, it checks only if the head of the course's list matches cond.
get_teaching_courses(CourseNumber, CourseList) :-
findall(Course,
(
professor(_, [Course | _]),
member(CourseNumber, Course)
),
CourseList).
professor fact has next struct:
professor(Name, [ [CourseName , CourseNumber], .... ]).
I am thinking of making predicate over predicate, but I can't achieve it (something wrong with append I guess).
It's been like 2 days I've started learning prolog, and if you can give me any help, advice or link that can help me, I'd appreciate it.
example:
assertz(
professor(
'Bob',
[
['Math', 2],
['PE', 3]
]
)
).
Solution:
get_teaching_courses(CourseNumber, CourseList) :-
findall(CourseName,
(
professor(_, Course),
member([CourseName, CourseNumber], Course)
),
CourseList).
thanks to #CapelliC and his answer.
the problem is the incorrect pattern matching applied in member/2. Try
member([_,CourseNumber], Course)

filter output of subprocess.check_output

I'm trying to match values of a list to a regex pattern. If the particular value within the list matches, I'll append it to a different list of dicts. If the above mentioned value does not match, I want to remove the value from the list.
import subprocess
def list_installed():
rawlist = subprocess.check_output(['yum', 'list', 'installed']).splitlines()
#print rawlist
for each_item in rawlist:
if "[\w86]" or \
"noarch" in each_item:
print each_item #additional stuff here to append list of dicts
#i haven't done the appending part yet
#the list of dict's will be returned at end of this funct
else:
remove(each_item)
list_installed()
The end goal is to eventually be able to do something similar to:
nifty_module.tellme(installed_packages[3]['version'])
nifty_module.dosomething(installed_packages[6])
Note to gnu/linux users going wtf:
This will eventually grow into a larger sysadmin frontend.
Despite the lack of an actual question in your post, I'll make a couple of comments.
You have a problem here:
if "[\w86]" or "noarch" in each_item:
It's not interpreted the way you think of it and it always evaluates to True. You probably need
if "[\w86]" in each_item or "noarch" in each_item:
Also, I'm not sure what you are doing, but in case you expect that Python will do regex matching here: it won't. If you need that, look at re module.
remove(each_item)
I don't know how it's implemented, but it probably won't work if you expect it to remove the element from rawlist: remove won't be able to actually access the list defined inside list_installed. I'd advise to use rawlist.remove(each_item) instead, but not in this case, because you are iterating over rawlist. You need to re-think the procedure a little (create another list and append needed elements to it instead of removing, for example).