Re: Difference between memberchk/2 and member/2...

Daniel Diaz <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
Le 19 juil. 2013 à 18:37, emacstheviking <[email protected]> a écrit :

> Given this rule:
> 
> is_digit(C) :- memberchk(C, "0123456789").
> 
> which used to use "member",
> 
> and given that I intend to be parsing a source language read from a
> file into a character list, what would be the more correct /
> appropriate one to use and why?
> 
> I know that member() can backtrack and therefore be used to iterate
> over a list but is that useful / relevant in my example rule ?

The difference between member/2 and memberchk/2 is that the former can be used both to check and to to generate digits while the later can only be used to check. In your case memberchk/2 is the best choice.

In generation mode, member/2 works:

| ?- member(X,"0123456789").     

X = 48 ? ;

X = 49 ? ;

X = 50 ? ;

X = 51 ? ;

X = 52 ? ;

X = 53 ? ;

X = 54 ? ;

X = 55 ? ;

X = 56 ? ;

X = 57

(1 ms) yes

memberchk/2 does not:

| ?- memberchk(X,"0123456789").

X = 48


Daniel

> 
> Thanks,
> Sean.
> 
> _______________________________________________
> Users-prolog mailing list
> [email protected]
> https://lists.gnu.org/mailman/listinfo/users-prolog
> 
> -- 
> Ce message a ete verifie par MailScanner
> pour des virus ou des polluriels et rien de
> suspect n'a ete trouve.
> 


-- 
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.