hello,
PROBLEM: pretty much what header says:
here is the programme, i created:
(defvar *list* (list "ant" "bat" "cat" "dat" "dit" "dot"))
;; 1. gives the user a list of strings.
;; 2. prompts the user for an element of the list input
;; 3. finds the position of that input in the list, else returns
"element not found"
(defun look-for-element ()
"ok we will create a list right-here"
(format t "~%this is the list we will work on: ~A~%" *list*)
(let ((element (prompt-read-element)))
(if (member element *list*)
(give-position element *list*)
(format nil "element not found"))))
;; prompts the user for input
(defun prompt-read-element ()
(format *query-io* "~%please enter the element you wish to search: ")
(force-output *query-io*)
(read-line *query-io*))
;; gives the position of an element in a string, of course starting
from zero.
(defun give-position (x a-list)
(cond ((null a-list) 0)
((equal (car a-list) x) 0)
(t (+ 1 (give-position x (cdr a-list))))))
both "prompt-read-element" & "give-position" work fine but
"look-for-element" doesn't. i am not able to spot the BUG. can you?
--------------------------------------------------------------------------------------------
original problem was solved in Java. i got it by "googling" . here is
this:
e362. Finding an Element in a Sorted List
// Create a list with an ordered list of strings
List sortedList = new LinkedList();
sortedList.addAll(Arrays.asList(new String[]{"ant", "bat", "cat",
"dog"}));
// Search for the word "cat"
int index = Collections.binarySearch(sortedList, "cat"); // 2
// Search for a non-existent element
index = Collections.binarySearch(sortedList, "cow"); // -4
A negative return value indicates that the element is not in the list.
However, the actual return value can be used to determine where that
non-existent element should be inserted in the list if that were
desired;
----------------------------------------------------------------------------------------------
thanks
--arnuld
ok i got the bug:
(member element *list*)
should be
(member element *list* :test #'string=)
;; now i know that Common Lisp uses "eq" as its default test.
thanks, anyway :-)
"arnuld"
From: Bulent Murtezaoglu
Subject: Re: finding the position of an element in a "list of strings"
Date:
Message-ID: <87r70zeykh.fsf@p4.internal>
>>>>> "arnuld" == arnuld <·······@gmail.com> writes:
[...]
arnuld> ;; now i know that Common Lisp uses "eq" as its default
arnuld> test.
No, it is eql. See CLHS 17.2.1 :
http://www.lispworks.com/documentation/HyperSpec/Body/17_ba.htm
I'll quote:
"If neither a :test nor a :test-not argument is supplied, it is as if
a :test argument of #'eql was supplied."
cheers,
BM
From: Takehiko Abe
Subject: Re: finding the position of an element in a "list of strings"
Date:
Message-ID: <keke-0607062045320001@192.168.1.2>
> ;; now i know that Common Lisp uses "eq" as its default test.
>
The default is #'eql . See "17.2.1 Satisfying a Two-Argument Test"
--
The original question, "Can machines think?"
I believe to be too meaningless to deserve discussion.
In article <························@m79g2000cwm.googlegroups.com>,
"arnuld" <·······@gmail.com> wrote:
> ;; gives the position of an element in a string, of course starting
> from zero.
> (defun give-position (x a-list)
> (cond ((null a-list) 0)
> ((equal (car a-list) x) 0)
> (t (+ 1 (give-position x (cdr a-list))))))
If you want the position, why don't you just use the built-in POSITION
function?
Also, it seems wasteful to call MEMBER before calling GIVE-POSITION,
since they both have to search the list. Just call POSITION, as it will
return NIL if the element isn't found, and the index otherwise.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Karsten Poeck
Subject: Re: finding the position of an element in a "list of strings"
Date:
Message-ID: <e8m8se$f92$1@news.ya.com>
I don't believe that give-position works, you are not treating the case that
the element is not found correctly.
just try:
(give-position 'this '(does not work))
(give-position 'this '(does this work))
Just look at your recursion, if you blindly add 1 + the recurevice value,
how will you treat the case that the element is not found.
You might try a helper-function with labels where you pass the current index
as an additional variable.
salud2
Karsten
>
> ;; gives the position of an element in a string, of course starting
> from zero.
> (defun give-position (x a-list)
> (cond ((null a-list) 0)
> ((equal (car a-list) x) 0)
> (t (+ 1 (give-position x (cdr a-list))))))
>
>
> both "prompt-read-element" & "give-position" work fine but
> "look-for-element" doesn't. i am not able to spot the BUG. can you?