在Win-Prolog中读取字符串并挑选出字符串中的每个单词 [英] Read string and single out every word in the string in Win-Prolog

查看:99
本文介绍了在Win-Prolog中读取字符串并挑选出字符串中的每个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在互联网上度过,但是我无法在Win-Prolog中找到任何可以断定字符串中每个单词的内置谓词.

I've spend whole day on the internet and I wasn't able to find any built-in predicate in Win-Prolog that can single out every word in a String.

示例:

| ?- read(X).
|: 'this is a string'.
X = 'this is a string'.

是否有任何我可以使用的谓词可以帮助我,挑出字符串中的每个单词?喜欢

is there any predicate I can use that will help me, single out every word of in the string? like

A = this
B = is
C = a
D = string

或列表

A = [This, is, a, string]

有可能吗?

推荐答案

在Win-Prolog中,您可以使用:

In Win-Prolog you might use:

split_string(String, List):-
  string_chars(String, LChar),
  split_string(List, LWord-LWord, LChar, []).

split_string([Word|TailWords], LWord-[])--> 
     " ", 
     {string_chars(Word, LWord)}, 
     split_string(TailWords, NLWord-NLWord).
split_string(Words, Head-[Char|LWord]) --> 
     [Char], 
     {[Char] \= " "}, 
     split_string(Words, Head-LWord).
split_string([Word], LWord-[])--> 
     [], 
     { string_chars(Word, LWord)}.

在SWI-Prolog中,您可以使用 atomic_list_concat(List,'','this is a string').

In SWI-Prolog you can use atomic_list_concat(List, ' ', 'this is a string').

这篇关于在Win-Prolog中读取字符串并挑选出字符串中的每个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆