write elements in a list in prolog - list

I am learning prolog. How do you write elements in a list in prolog where the list may contains elements beginning with a capital letter.
for example: I have the predicate my_write/1
my_write([]). /* Base case: An empty list */
my_write([X|R]):- write(X),nl,my_write(R). /* Recursive case: */
But when I run my_write([How, are, you]). I get [_G749,are,you]
I know that words that begin with capital letter are variables in prolog.
I know you could enclose the word in a list that begins with capital letter in quotes, but is it possible to do it without having to do that.

I don't think there is a (better) way to do it instead of my_write(['How', are, you]).
BTW, you need to quote not only terms starting with a capital letter, but also terms starting with an underscore sign, or terms with spaces in their names.

SWI-Prolog has code_type/2 to handle character duties, with a not very user friendly syntax.
If you need to make upper case the first letter of an atom:
upcase_first_char(Plain, Proper) :-
atom_codes(Plain, [First|Cs]),
code_type(First, to_lower(Upcase)),
atom_codes(Proper, [Upcase|Cs]).
yields
?- upcase_first_char(carlo, V).
V = 'Carlo'.
to be used, in your case, like
my_write([First|Rest]) :-
upcase_first_char(First, Upper),
maplist(writeln, [Upper|Rest]).
yields
?- my_write([how, are, you]).
How
are
you

Related

What is the regular expression that generates the language where every odd position in the string is an a?

What is the regular expression that generates the language where every odd position in the string is an a? (Please answer with the shortest possible regex: minimal parentheses, no spaces and any piped strings in alphabetical order!)
I assume I'm working with only a's and b's.
(a(a|b))+ would only cover even strings: a(a|b), a(a|b)a(a|b), etc.
How do I also cover the case that the string is odd? ex: a(a|b)a
Note: not using programming syntax
Edit: some valid strings would be: a, aa, aaa, aaaa, aaaaa, ab, aba, abab, ababa, etc.
EDIT: Solution
My instructor gave the answer (aa|ab)*. This is incorrect because it misses case(s), for example "a".
I think this suits your requirement:
^a(.a)*.?$
Position 1 must be "a": ^a
Repetitions of any character + a, making a sequence where odds are "a"'s: (.a)*
Allowing for a termination not ending in "a", ex abab: .?$
You can check it here: regex101
^(a.)*a?$
Allows empty values ("")
From start to end of line (^...$)
Every odd place (1,3,5,...) is an a followed by any letter/number
There may or may not be an a in the end
One sign shorter thay Jorge's answer, but allows empty values
See regex101 example here
I think this might help you
Case 1: even length strings (1(0+1))*
Case 2: odd length strings 1((0+1)1)*
Finally the answer is Case 1 + Case 2

How to check if a certain pattern exists in a list in prolog

i was trying to implement a prolog predict that checks if a certain pattern say (x,m) exists in a list or m exists at the very end of the list and count the number of its occurrence i never get an answer to the number of times the pattern existed.why?
my attempt was :
certainP([_,m],RESULT,W):-
W is RESULT+1.
certainP([x,m|T],START,RESULT):-
RESULT is START+1,
START is RESULT,
certainP(T,START,RESULT).
This should do what you describe:
certainP([],N,N).
certainP([_],N,N).
certainP([x,m|T],N,N2) :-
N1 is N+1,
certainP(T,N1,N2).
certainP([_|T],N,N2) :-
certainP(T,N,N2).
This assumes that the middle argument is provided an initial numeric value in the query.

Prolog find element in list of lists with preference

I have list of lists like:
L = [[Q,w,E,],[Q,w,Z,r],[A,s,D,f]]
I know the first two and I need to get the rest.
For example I have Q,w and I need to get Z,r or E,r.
I would like to somehow tell that with priority I always want that touple contain Z, but if doesnt exist give me E,r.
I tried:
member([Q,w,Z,VB],[[Q,w,E,o],[Q,w,Z,r],[A,s,D,f]]).
But that always give me Z = E, VB = o
First you need to know the difference in Prolog between an Atom and a Variable, you can read about their syntax here
Now, if you want a list of atoms that begin with an upper-case letter, you must enclose them in single quotes, otherwise prolog will interpret them as variables.
Now if you fix the syntax of your consult, you will get the following result:
?- member(['Q',w,Z,VB],[['Q',w,'E',o],['Q',w,'Z',r],['A',s,'D',f]]).
VB = o,
Z = 'E'
VB = r,
Z = 'Z'
false
Note how in this case I enclosed in single quote all atoms beginning with an upper-case letter, except for Z and VB in the first argument of the member/2 predicate, cause in this case they function as variables to be instanciated by prolog with the atoms needed to complete this case.

Subsetting a string based on pre- and suffix

I have a column with these type of names:
sp_O00168_PLM_HUMAM
sp_Q8N1D5_CA158_HUMAN
sp_Q15818_NPTX1_HUMAN
tr_Q6FGH5_Q6FGH5_HUMAN
sp_Q9UJ99_CAD22_HUMAN
I want to remove everything before, and including, the second _ and everything after, and including, the third _.
I do not which to remove based on number of characters, since this is not a fixed number.
The output should be:
PLM
CA158
NPTX1
Q6FGH5
CAD22
I have played around with these, but don't quite get it right..
library(stringer)
str_sub(x,-6,-1)
That’s not really a subset in programming terminology1, it’s a substring. In order to extract partial strings, you’d usually use regular expressions (pretty much regardless of language); in R, this is accessible via sub and other related functions:
pattern = '^.*_.*_([^_]*)_.*$'
result = sub(pattern, '\\1', strings)
1 Aside: taking a subset is, as the name says, a set operation, and sets are defined by having no duplicate elements and there’s no particular order to the elements. A string by contrast is a sequence which is a very different concept.
Another possible regular expression is this:
sub("^(?:.+_){2}(.+?)_.+", "\\1", vec)
# [1] "PLM" "CA158" "NPTX1" "Q6FGH5" "CAD22"
where vec is your vector of strings.
A visual explanation:
> gsub(".*_.*_(.*)_.*", "\\1", "sp_O00168_PLM_HUMAM")
[1] "PLM"

Regular expressions, what a trouble!

I need your kind help to resolve this question.
I state that I am not able to use regolar expressions with Oracle PL/SQL, but I promise that I'll study them ASAP!
Please suppose you have a table with a column called MY_COLUMN of type VARCHAR2(4000).
This colums is populated as follows:
Description of first no.;00123457;Description of 2nd number;91399399119;Third Descr.;13456
You can see that the strings are composed by couple of numbers (which may begin with zero), and strings (containing all alphanumeric characters, and also dot, ', /, \, and so on):
Description1;Number1;Description2;Number2;Description3;Number3;......;DescriptionN;NumberN
Of course, N is not known, this means that the number of couples for every record can vary from record to record.
In every couple the first element is always the number (which may begin with zero, I repeat), and the second element is the string.
The field separator is ALWAYS semicolon (;).
I would like to transform the numbers as follows:
00123457 ===> 001-23457
91399399119 ===> 913-99399119
13456 ===> 134-56
This means, after the first three digits of the number, I need to put a dash "-"
How can I achieve this using regular expressions?
Thank you in advance for your kind cooperation!
I don't know Oracle/PL/SQL, but I can provide a regex:
([[:digit:]]{3})([[:digit:]]+)
matches a number of at least four digits and remembers the first three separately from the rest.
RegexBuddy constructs the following code snippet from this:
DECLARE
result VARCHAR2(255);
BEGIN
result := REGEXP_REPLACE(subject, '([[:digit:]]{3})([[:digit:]]+)', '\1-\2', 1, 0, 'c');
END;
If you need to make sure that those numbers are always directly surrounded by ;, you can alter this slightly:
(^|;)([[:digit:]]{3})([[:digit:]]+)(;|$)
However, this will not work if two numbers can directly follow each other (12345;67890 will only match the first number). If that's not a problem, use
result := REGEXP_REPLACE(subject, '(^|;)([[:digit:]]{3})([[:digit:]]+)(;|$)', '\1\2-\3\4', 1, 0, 'c');