I need to solve homework, but I have very limited knowledge of Prolog. The task is as follows:Write a Prolog program that can list all the substrings of a string whose length is at least two characters, and the first and last characters are the same.
For instance:
?- sameend("teletubbies", R). R = "telet"; R = "ele"; R = "eletubbie"; R = "etubbie"; R = "bb"; false.
My approach to this problem is that I have to iterate over the string with head / tail and find the index of the next letter that matches the current one (it satisfies the minimum requirement of 2 lengths) and cuts the substring using a predicate sub_string.
sub_string
, . Prolog . , , . . .
:- set_prolog_flag(double_quotes, chars). sameend(Xs, Ys) :- phrase( ( ..., [C], seq(Zs), [C], ... ), Xs), phrase( ( [C], seq(Zs), [C] ), Ys). ... --> [] | [_], ... . seq([]) --> []. seq([E|Es]) --> [E], seq(Es).
Prolog append/2 last/2 (lists), ,
sameend(S,[F|T]) :- append([_,[F|T],_],S),last(T,F).
Source: https://habr.com/ru/post/1674897/More articles:Webpack2 Angular2 ng-bootstrap Tree Shaking - angularApply function to value inside IO Perhaps - haskelljwt token multiple rentals - asp.net-mvcPython-like.format () in php - pythonC ++ - interfaces, inheritance, polymorphism - c ++Using a non-standard editor with / edit - java-9Is the static statics of the class template created differently in shared libraries consistent in the final binary format? - c ++Запустить веб-страницу Flash в безголовом Chrome на Ubuntu 14.04 - google-chromeScala - understand class definition with Bounds - operatorsWhy does git help add "git -" before each command name? - gitAll Articles