hello,
PROBLEM: pretty much what header says:
both "prompt-read-element" & "give-position" work fine but
"look-for-element" doesn't. i am not able to spot the BUG. can you?
(defvar *list* (list "ant" "bat" "cat" "dat" "dit" "dot"))
(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))))))
--------------------------------------------------------------------------------------------
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
"arnuld" <·······@gmail.com> writes:
> hello,
>
> PROBLEM: pretty much what header says:
>
>
> both "prompt-read-element" & "give-position" work fine but
> "look-for-element" doesn't. i am not able to spot the BUG. can you?
>
>
> (defvar *list* (list "ant" "bat" "cat" "dat" "dit" "dot"))
>
> (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))))))
(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))
(pos (position element *list* :test (function string=))))
; or (function string-equal) for case insensivity
(format t "~:[Element not found~;Position is ~:*~D~]~%" pos)
pos))
Ain't it embarrasing?
Instead of googling for a "Java Solution", you'd better read CLHS or
any Common Lisp tutorial.
http://www.lispworks.com/documentation/HyperSpec/Front/Contents.htm
--
__Pascal Bourguignon__ http://www.informatimago.com/
"This machine is a piece of GAGH! I need dual Opteron 850
processors if I am to do battle with this code!"
Pascal Bourguignon wrote:
> (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))
> (pos (position element *list* :test (function string=))))
> ; or (function string-equal) for case insensivity
> (format t "~:[Element not found~;Position is ~:*~D~]~%" pos)
> pos))
>
> Ain't it embarrasing?
hell..a..lot
well, i didnt know, my code looks so much bad... sigs of a programming
newbie :-(
> Instead of googling for a "Java Solution", you'd better read CLHS or
> any Common Lisp tutorial.
> http://www.lispworks.com/documentation/HyperSpec/Front/Contents.htm
Hyperspec includes all the theory. Oh yes, ther are code examples &
they seem like some cumbersome non-sense thrown to me, for the moment.
"arnuld"
"arnuld" <·······@gmail.com> writes:
> Pascal Bourguignon wrote:
>> (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))
>> (pos (position element *list* :test (function string=))))
>> ; or (function string-equal) for case insensivity
>> (format t "~:[Element not found~;Position is ~:*~D~]~%" pos)
>> pos))
>>
>> Ain't it embarrasing?
>
> hell..a..lot
>
> well, i didnt know, my code looks so much bad... sigs of a programming
> newbie :-(
>
>> Instead of googling for a "Java Solution", you'd better read CLHS or
>> any Common Lisp tutorial.
>> http://www.lispworks.com/documentation/HyperSpec/Front/Contents.htm
>
> Hyperspec includes all the theory. Oh yes, ther are code examples &
> they seem like some cumbersome non-sense thrown to me, for the moment.
Actually, you don't need to know CLHS by heart. But you need to know
what's in it! As a newbie, you can learn more reading a tutorial or a
book about Common Lisp than the Reference. But browsing the Reference
from time to time let you avoid re-implementing a function that's
already exported from COMMON-LISP.
--
__Pascal Bourguignon__ http://www.informatimago.com/
NEW GRAND UNIFIED THEORY DISCLAIMER: The manufacturer may
technically be entitled to claim that this product is
ten-dimensional. However, the consumer is reminded that this
confers no legal rights above and beyond those applicable to
three-dimensional objects, since the seven new dimensions are
"rolled up" into such a small "area" that they cannot be
detected.
Pascal Bourguignon wrote:
> Actually, you don't need to know CLHS by heart. But you need to know
> what's in it! As a newbie, you can learn more reading a tutorial or a
> book about Common Lisp than the Reference.
i am not a *pure* newbie, i already did 22 chapters from PCL, very
tough, complex & confusing & in the end i made it. At chapter 23 Peter
Seibel starts to implement a spam-filter which was just above my head
(i mean his statistical probalities, inverse-chi square etc etc).
whether i read or do not read his probabilites it doesnt make any
difference to whole chapter. i tried this chapter aroud 8 times but
never made it & so is with other practicals(id-parser, HTML interpreter
& compiler etc). I know practicals are important but i am not able to
complete them *with* understanding, always got confused & forgot what i
read on previuos pages.
do you have any advice on all this?
it seems to me that this book assumes reader has done some real-life
programming at some point (so i am not the guy here).
> But browsing the Reference
> from time to time let you avoid re-implementing a function that's
> already exported from COMMON-LISP.
got it :-)
thanks
"arnuld"
"arnuld" <·······@gmail.com> writes:
> Pascal Bourguignon wrote:
>> Actually, you don't need to know CLHS by heart. But you need to know
>> what's in it! As a newbie, you can learn more reading a tutorial or a
>> book about Common Lisp than the Reference.
>
> i am not a *pure* newbie, i already did 22 chapters from PCL, very
> tough, complex & confusing & in the end i made it. At chapter 23 Peter
> Seibel starts to implement a spam-filter which was just above my head
> (i mean his statistical probalities, inverse-chi square etc etc).
> whether i read or do not read his probabilites it doesnt make any
> difference to whole chapter. i tried this chapter aroud 8 times but
> never made it & so is with other practicals(id-parser, HTML interpreter
> & compiler etc). I know practicals are important but i am not able to
> complete them *with* understanding, always got confused & forgot what i
> read on previuos pages.
>
> do you have any advice on all this?
Well, _practical_ computer programming involves necessarily some
_practical_ knowledge, out of the computer programming theory.
To implement a spam filter, you need to know some practical statistics
and probabilities.
Actually, the probabilities behind a Bayesian filter are quite simple.
http://en.wikipedia.org/wiki/Bayesian_filtering
> it seems to me that this book assumes reader has done some real-life
> programming at some point (so i am not the guy here).
Indeed. For complete programmer-newbies, I'd advise rather:
Common Lisp: A Gentle Introduction to Symbolic Computation
http://www.cs.cmu.edu/~dst/LispBook/
--
__Pascal Bourguignon__ http://www.informatimago.com/
CONSUMER NOTICE: Because of the "uncertainty principle," it is
impossible for the consumer to simultaneously know both the precise
location and velocity of this product.
Pascal Bourguignon wrote:
> Well, _practical_ computer programming involves necessarily some
> _practical_ knowledge, out of the computer programming theory.
>
> To implement a spam filter, you need to know some practical statistics
> and probabilities.
>
> Actually, the probabilities behind a Bayesian filter are quite simple.
> http://en.wikipedia.org/wiki/Bayesian_filtering
ok, i will read this one.
> > it seems to me that this book assumes reader has done some real-life
> > programming at some point (so i am not the guy here).
>
> Indeed. For complete programmer-newbies, I'd advise rather:
>
> Common Lisp: A Gentle Introduction to Symbolic Computation
> http://www.cs.cmu.edu/~dst/LispBook/
i did read it & this was the book that hooked me on Lisp rather than
Ruby/SCHEME. I always thought i am a naturally talented person, yes
even after reading Touretzky, Touretzky is far away from Practical
Programming. my all beliefs broke when i read PCL and i today i accpet
myself as an average man :-)