如何在Prolog中正确调用括号列表元素

huangapple go评论81阅读模式
英文:

How to properly call parentheses list elements in Prolog

问题

你试图做的是检查元素'mixcoac'是否属于linea(12)的数组,这个数组是第二个元素。但是在Prolog语法中,你需要使用方括号来访问列表的元素。

你可以这样做:

member(mixcoac, linea(12)).

这将检查'mixcoac'是否在linea(12)的列表中,如果是的话,它将返回true。

英文:

Regards, i have the next knowledge base:

linea(1,[observatorio,tacubaya,juanacatlan,chapultepec,sevilla,cuahtemoc,
	 balderas,salto_del_agua,isabel_la_catolica,pino_suarez,merced,
	 candelaria,san_lazaro,moctezuma,balbuena,bulevard_puerto_aereo,
	 gomez_farias,zaragoza,pantitlan]
linea(2,[cuatro_caminos,panteones,tacuba,cuitlahuac,popotla,colegio_militar,
	 normal,san_cosme,revolucion,hidalgo,bellas_artes,allende,zocalo,
	 pino_suarez,san_antonio_abad,chabacano,viaducto,xola,villa_de_cortes,
	 nativitas,portales,ermita,general_anaya,tasquena]).
linea(3,[indios_verdes,deportivo_18_de_marzo,potrero,la_raza,tlatelolco,
	 guerrero,hidalgo,juarez,balderas,nignos_heroes,hospital_general,
	 centro_medico,etiopia,eugenia,division_del_norte,zapata,coyoacan,
	 viveros,miguel_angel_de_quevedo,copilco,universidad]).
linea(4,[martin_carrera,talisman,bondojito,consulado,canal_del_norte,
	 morelos,candelaria,fray_servando,jamaica,santa_anita]).
linea(5,[politecnico,instituto_del_petroleo,autobuses_norte,la_raza,misterios,
	 valle_gomez,consulado,eduardo_molina,aragon,oceania,terminal_aerea,
	 hangares,pantitlan]).
linea(6,[el_rosario,tezozomoc,azcapotzalco,ferreria,norte_45,vallejo,
	 instituto_del_petroleo,lindavista,deportivo_18_de_marzo,basilica,
	 martin_carrera]).
linea(7,[el_rosario,aquiles_serdan,camarones,refineria,tacuba,
	 rio_san_joaquin,polanco,auditorio,constituyentes,tacubaya,
	 san_pedro_de_los_pinos,san_antonio,mixcoac,barranca_del_muerto]).
linea(8,[garibaldi,bellas_artes,san_juan_de_letran,salto_del_agua,
	 doctores,obrera,chabacano,la_viga,santa_anita,coyuya,iztacalco,
	 apatlaco,aculco,escuadron_201,atlalilco,iztapalapa,
	 cerro_de_la_estrella,uami,constitucion_de_1917]).
linea(9,[tacubaya,patriotismo,chilpancingo,centro_medico,lazaro_cardenas,
	 chabacano,jamaica,mixihuca,velodromo,ciudad_deportiva,puebla,
	 pantitlan]).
linea(a,[pantitlan, agricola_oriental,canal_de_san_juan,tepalcates,guelatao,
	 pegnon_viejo,acatitla,santa_marta,los_reyes,la_paz]).
linea(b,[ciudad_azteca,plaza_aragon,olimpica,ecatepec,muzquiz,
	 rio_de_los_remedios,impulsora,nezahualcoyotl,villa_de_aragon,
	 bosque_de_aragon,deportivo_oceania,oceania,romero_rubio,
	 r_flores_magon,san_lazaro,morelos,tepito,lagunilla,garibaldi,
	 guerrero,buenavista]).
linea(12,[mixcoac,insurgentes_sur,hospital_20_de_noviembre,zapata,
	  parque_de_los_venados,eje_central,ermita,mexicaltzingo,
	  atlalico,culhuacan,san_andres_tomatlan,lomas_estrella,calle_11,
	  periferico_oriente,tezonco,olivos,nopalera,zapotitlan,
	  tlaltenco,tlahuac]).

... and the next functions:

member(X,[X|Xs]).
member(X,[Y|Xs]):-member(X,Xs).

append([],X,X).
append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs).

What i'm trying to do is to check, for example, if the element 'mixcoac' belongs to the linea(12)'s array which is something like the 2nd element, but i don't know how to work this kind of lists in the Prolog syntax.

I tried to execute member(mixcoac, 12). but that doesn't work.

答案1

得分: 1

这个查询在第一次成功,然后在回溯时失败。这意味着元素mixcoaclinea(12, L)的列表中只出现一次。

上面显示的清单是特定于SWI-Prolog的,以及我们使用的顶层(交互式shell),以及默认配置。这意味着其他Prolog可能会看起来不同,以不同的方式运行代码会有所不同。例如,你可以使用“匿名变量”,以下划线开始,用于列表;你还可以配置SWI-Prolog的顶层以不显示匿名变量的绑定。不显示任何其他内容时,你将得到"true":

?- set_prolog_flag(toplevel_print_anon, false).
true.

?- linea(12, _L), member(mixcoac, _L).
true .

我还在"true"后键入Enter键,而不是";"。这样,我没有请求更多的解决方案,也没有得到"false",即"没有更多的解决方案"。

这些内容与你的原始问题完全无关。

此外,从你的问题中:

但我不知道如何在Prolog语法中处理这种类型的列表。

这似乎与语法无关。一些通用教程应该已经教会了你所需知道的一切。

英文:

Like this:

?- linea(12, L), member(mixcoac, L).
L = [mixcoac, insurgentes_sur, hospital_20_de_noviembre, zapata, parque_de_los_venados, eje_central, ermita, mexicaltzingo, atlalico|...] ;
false.

This query succeeds once and fails on backtracking. This means that the element mixcoac is seen once in the list of linea(12, L).


The listing I show above is specific to SWI-Prolog, and the fact we are using the toplevel (the interactive shell), and how it is configured by default. This means that other Prologs will look different, and running the code differently will look different. For example, you can use "anonymous variables", starting with underscore, for the list; and you can configure SWI-Prolog's toplevel to not show the bindings for anonymous variables. With nothing else to show, you will get your "true":

?- set_prolog_flag(toplevel_print_anon, false).
true.

?- linea(12, _L), member(mixcoac, _L).
true .

I also typed <kbd>Enter</kbd> instead of <kbd>;</kbd> after the "true". This way I didn't ask for more solutions and I didn't get a "false" ie "there are no more solutions".

All of this under the line is completely irrelevant to your original question.

Also, from your question:

> but i don't know how to work this kind of lists in the Prolog syntax.

This has nothing to do with syntax it feels. Some generic tutorial should have taught you all you need to know.

huangapple
  • 本文由 发表于 2023年3月7日 15:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定