checking item from input list

Discussion of Common Lisp
Post Reply
whiteunibrow
Posts: 3
Joined: Tue Mar 22, 2011 11:48 am

checking item from input list

Post by whiteunibrow » Tue Mar 22, 2011 11:56 am

Hi,
I made the following function:

Code: Select all

(defun getInputTypeRead ()
  (let ((inputSeq (read)))
  (cond
    ((EQ inputSeq nil) 0)
    ((not (equal (first inputSeq) 'bees)) 0)
    (T 1))))
with input list typed in as
(bees are nice)
I want this to come up with a value of 1. I've tried EQ, equal, equalp, and none of them seem to be working; they've been evaluating to 0.
Basically the input has to come in in this form, and i want to check if the first word is "bees" without the quotes. Just the symbol bees.

If I change the line to
((not (equalp (first inputSeq) "bees")) 0)
and the input to ("bees" are nice)
then it evaluates to 1 but I don't want that type of input.

I hope the question is clear, and thanks!

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: checking item from input list

Post by gugamilare » Tue Mar 22, 2011 1:52 pm

I think you made a mistake when you copied the function and posted it.

Anyway, if I understand what you want, you should be use find

Code: Select all

(defun getInputTypeRead ()
  (let ((inputSeq (read)))
  (cond
    ((EQ inputSeq nil) 0)
    ((find 'bees inputSeq) 1)
    (T 0))))

whiteunibrow
Posts: 3
Joined: Tue Mar 22, 2011 11:48 am

Re: checking item from input list

Post by whiteunibrow » Tue Mar 22, 2011 2:01 pm

What mistake? I don't think there is one, and
No, that's not what I want to do. I exactly want to return 0 if the list inputSeq is nil or if the first element is something other than bees, otherwise return 1. That is, return 1 if the first element exists and is bees.
Find will return 1 if the item is present anywhere in the list whereas i just want to check if the first element is bees, exactly as typed in the command line as shown in my first post, not the string "bees".

jstoddard
Posts: 20
Joined: Fri Jan 28, 2011 6:13 pm

Re: checking item from input list

Post by jstoddard » Tue Mar 22, 2011 2:17 pm

Code: Select all

[8]> (defun getInputTypeRead () (let ((inputSeq (read))) (cond ((EQ inputSeq nil) 0) ((not (eq (first inputSeq) 'bees)) 0) (T 1))))
GETINPUTTYPEREAD
[9]> (getInputTypeRead)
(bees are nice)
1
[10]> (getInputTypeRead)
("bees" are nice)
0
[11]> (getInputTypeRead)
(hello world)
0
[12]> 
Odd; that's the results you're looking for, right? I just threw that into the repl in clisp without any problems...

whiteunibrow
Posts: 3
Joined: Tue Mar 22, 2011 11:48 am

Re: checking item from input list

Post by whiteunibrow » Tue Mar 22, 2011 2:34 pm

Odd, yes that is exactly what I am looking for. What's weird is when I copy and paste the function in your post into REPL and change the name to getInputTypeRead1 and then invoke the function, I get the desired results, the same you got. However, I am still getting the incorrect results for when I type (myLispProject:getInputTypeRead) into REPL. What could be wrong? I have the symbol exported and I keep loading the asd file, which I don't even know is necessary but I am doing it anyway just in case.

vanekl
Posts: 12
Joined: Wed Dec 15, 2010 10:25 am

Re: checking item from input list

Post by vanekl » Wed Mar 23, 2011 5:25 am

I have the symbol exported and I keep loading the asd file, which I don't even know is necessary but I am doing it anyway just in case.
ASDF normally doesn't reload code if you ask it to after it's been loaded once. To force it to reload you have to use the :force t option.

FAU

Re: checking item from input list

Post by FAU » Wed Mar 23, 2011 11:51 am

I assume you have something like that as you mentioned you put your function in its own package:

Code: Select all

(defpackage :my-lisp-project
  (:use :cl)
  (:export :get-input-type-read))

(in-package :my-lisp-project)

(defun get-input-type-read ()
  (let ((input-seq (read)))
    (cond
      ((eq input-seq nil) 0)
      ((not (equal (first input-seq) 'bees)) 0)
      (t 1))))
The problem is that you likely compare two distinct symbols which happen to have the same print name BEES.

Keep in mind when you call your function on the repl, READ interns the symbols into the current package which is CL-USER (unless you changed that).

You can investigate this problem further on the repl:

CL-USER> (my-lisp-project:get-input-type-read)
(bees buzz)
0

CL-USER> (find-symbol "BEES")
BEES
:INTERNAL

CL-USER> (find-symbol "BEES" :my-lisp-project)
MY-LISP-PROJECT::BEES
:INTERNAL

CL-USER> (eq 'bees 'my-lisp-project::bees)
NIL

Here you can see BEES in CL-USER is not BEES in MY-LISP-PROJECT::BEES! Now lets make it work on the repl.

CL-USER> (unintern 'bees)
T

CL-USER> (import 'my-lisp-project::bees)
T

CL-USER> (eq 'bees 'my-lisp-project::bees)
T

CL-USER> (my-lisp-project:get-input-type-read)
(bees buzz)
1

You can avoid this kind of problem by using keywors as they always get interned into the keywords package.

Post Reply