从术语列表中提取子项

时间:2016-05-25 13:05:29

标签: prolog

我的问题可能很容易解决,但今天我有一个“可怜”的日子。 我试图从url中提取主机。函数parse_url来自库(url)。

这是一个功能:

extract_host(X) :-
    parse_url("http://hostexample.org/index.html", X).

输出:

  

X = [protocol(http),host('hostexample.org'),path('/ index.html')]。

如何从中获取 hostexample.org

编辑(工作功能):

extract_host(HN) :-
    parse_url("http://hostexample.org/index.html", X),
    memberchk(host(HN), X).

1 个答案:

答案 0 :(得分:2)

要获取主机,您应该使用parse_url/2调用统一列表中的memberchk/2,因为主机可能是也可能不是它的第二个参数(parse_url/2的文档没有说明该列表中参数的顺序。)

extract_host(Url, HostName):-
    parse_url(Url, LParsed),
    memberchk(host(HostName), LParsed).

并查询extract_host("http://hostexample.org/index.html", Host).

相关问题