Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:17:11+00:00 2026-06-16T00:17:11+00:00

I want to check if a character exists in a string. So Atom is

  • 0

I want to check if a character exists in a string. So Atom is the string and Ch the character. name is a predicate that converts the string in a list of numbers according to the ASCII code.

find_element is a predicate that is supposed to be true only if element X is part of a list. C is a counter that tells us where exactly element X was found.

This is the result I am getting:

?- exists(prolog,g). [103][112,114,111,108,111,103] false.

——-> 103 is the ASCII code of letter “g” and the list [112,114,111,108,111,103] is the list that represents the string “prolog”. The question exists(prolog,g) should have provided a true response.

However the find_element predicate is working correctly. I don’t understand why this is happening because when I type for example

?- find_element(5,[3,4,5,6,5,2],X).

I am getting X= 3 ; X = 5 ; false.
—->

which is absolutely fine because it tells me that 5 is the 3rd and the 5th element of the list.

So the problem is that find_element is working when I type something like ?- find_element(5,[3,4,5,6,5,2],X) but it is not when I try to call the predicate exists (which calls find_element).

This is the code:

find_element(X,[X|T],1).

find_element(X,[H|T],C):- find_element(X,T,TEMPC), C is TEMPC +1.

exists(Atom,Ch):- name(Atom,[X|T]), name(Ch,Z), write(Z), write([X|T]), find_element(Z,[X|T],Count).

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:17:12+00:00Added an answer on June 16, 2026 at 12:17 am

    I’ve cleaned a bit your code, and fixed a bug:

    find_element(X,[X|_], 1).
    find_element(X,[_|T], C) :-
        find_element(X,T,TEMPC),
        C is TEMPC +1.
    
    exists(Atom, Ch):-
        name(Atom, L),
        name(Ch, [Z]),
        find_element(Z, L, _Count).
    

    note name(Ch, [Z]) to extract the single character. Now

    ?- exists(pippo,o).
    true
    

    It’s worth to note that

    ?- find_element(3, [1,2,3,4,1,2,3,4],P).
    P = 3 ;
    P = 7 ;
    false.
    
    ?- nth1(P, [1,2,3,4,1,2,3,4], 3).
    P = 3 ;
    P = 7 ;
    false.
    

    your find_element/3 behaves as nth1/3, with arguments 1 and 3 swapped.

    Of course there are simpler and more general ways to perform such test. Using ISO builtins
    like sub_atom/5 (a really powerful primitive for atom inspection)

    ?- sub_atom(pippo, _,_,_, o).
    true ;
    

    or memberchk/2, after the conversion to character lists that you already know (but using ISO builtin atom_codes/2)

    exists(Atom, Ch):-
        atom_codes(Atom, L),
        atom_codes(Ch, [Z]),
        memberchk(Z, L).
    

    To count occurrences of a sub_atom, library(aggregate) can be used

    occurences(Atom, Ch, N) :-
      aggregate_all(count, sub_atom(Atom, _,_,_, Ch), N).
    
    ?- occurences(pippo, p, X).
    X = 3.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check that Java String or character array is not just made
I want to check the last character of the String for characters that are
I want to check if my string contains a + character.I tried following code
I want to check spans for the character that is printed when you hold
I have a list of strings and with each string I want to check
I have written a code that searches specific string from database table.what i want
I want to I check whether a string is in ASCII or not. I
So I want to check if the First character of a String is a
I don't want check if an item with a value exists in the arraylist,
I want to check a string to allow only a-z A-Z 0-9 . /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.