From: Mikael Jansson
Subject: LOOP probblems
Date: 
Message-ID: <38f4fe12-cbbc-498d-9181-519f36fc76a5@z38g2000hsc.googlegroups.com>
Trying to get the following code to work, but SBCL tells me it has
deleted unreachable code, namely the GETHASH stuff. Obviously I'm
confused about the syntax of LOOP, but not even the "Tutorial for the
Common Lisp Loop Macro" couldn't help me!  I get the feeling the
things I'm doing with LET-bindings are built into LOOP already, but I
don't know how to unleash its powers yet...

The code::

  (defun unifiable-words-p (scrambled plain)
    (let ((mappings (make-hash-table)))
      (loop for s across scrambled
            for p across plain
            do (let ((v (gethash c 'mappings)))
                 (when (magic-fails v s p))
                   return nil))
            ;; otherwise(?)
            return t)))

Any clues on how to achieve what I'm trying to do? Sorry for not
providing more detail; the code is from memory (no 'net access from
the code computer).

Thanks.
--
Mikael Jansson
http://mikael.jansson.be

From: Pascal Costanza
Subject: Re: LOOP probblems
Date: 
Message-ID: <65qf71F2goah9U1@mid.individual.net>
Mikael Jansson wrote:
> Trying to get the following code to work, but SBCL tells me it has
> deleted unreachable code, namely the GETHASH stuff.

The warnings about unreachable code in SBCL are misleading. You should 
better ignore them, they are only helpful for experts in some corner 
cases. Just because SBCL says that it deleted unreachable code doesn't 
mean that the code is gone. Don't ask. :}

> Obviously I'm
> confused about the syntax of LOOP, but not even the "Tutorial for the
> Common Lisp Loop Macro" couldn't help me!  I get the feeling the
> things I'm doing with LET-bindings are built into LOOP already, but I
> don't know how to unleash its powers yet...
> 
> The code::
> 
>   (defun unifiable-words-p (scrambled plain)
>     (let ((mappings (make-hash-table)))
>       (loop for s across scrambled
>             for p across plain
>             do (let ((v (gethash c 'mappings)))
>                  (when (magic-fails v s p))
>                    return nil))
>             ;; otherwise(?)
>             return t)))

You don't refer to lexical variables, like mappings, by quoting them in 
references. Don't say (gethash c 'mappings), just say (gethash c mappings).

Apart from that it's hard to understand what you actually want to 
achieve. So what do you actually want to achieve?

My guess is something like this:

(defun unifiable-word-p (scrambled plain)
   (loop with mappings = (make-hash-table)
         for s across scrambled
         for p across plain
         for v = (gethash c mappings) ;; where does c come from?!?
         when (magic-fails v s p)
         return nil
         finally (return t)))

...but I'm just guessing...


Pascal

-- 
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Zach Beane
Subject: Re: LOOP probblems
Date: 
Message-ID: <m3d4p3kfvy.fsf@unnamed.xach.com>
Pascal Costanza <··@p-cos.net> writes:

> Mikael Jansson wrote:
>> Trying to get the following code to work, but SBCL tells me it has
>> deleted unreachable code, namely the GETHASH stuff.
>
> The warnings about unreachable code in SBCL are misleading. You should
> better ignore them, they are only helpful for experts in some corner
> cases. Just because SBCL says that it deleted unreachable code doesn't
> mean that the code is gone. Don't ask. :}

I find that when I get unreachable code warnings in SBCL, it's almost
always because of an error I need to correct. In several years of
using SBCL I've run into only a couple spurious cases of the
warning. I'd suggest anyone who gets unreachable code warnings in SBCL
should learn what they mean and how to correct whatever triggers them.

Zach
From: Mikael Jansson
Subject: Re: LOOP probblems
Date: 
Message-ID: <d8aee52f-a0d7-4cb4-9fe8-3688a99af6b7@d45g2000hsc.googlegroups.com>
On 6 Apr, 01:10, Pascal Costanza <····@p-cos.net> wrote:
> Mikael Jansson wrote:
>
> > Obviously I'm
> > confused about the syntax of LOOP, but not even the "Tutorial for the
> > Common Lisp Loop Macro" couldn't help me!  I get the feeling the
> > things I'm doing with LET-bindings are built into LOOP already, but I
> > don't know how to unleash its powers yet...
>
> > The code::
>
> >   (defun unifiable-words-p (scrambled plain)
> >     (let ((mappings (make-hash-table)))
> >       (loop for s across scrambled
> >             for p across plain
> >             do (let ((v (gethash c 'mappings)))
> >                  (when (magic-fails v s p))
> >                    return nil))
> >             ;; otherwise(?)
> >             return t)))
>
> You don't refer to lexical variables, like mappings, by quoting them in
> references. Don't say (gethash c 'mappings), just say (gethash c mappings).
>
I don't understand. Are you saying I should never quote the hash
parameter to gethash? I just remember it being quoted last time I
looked at gethash, but obviously I was mistaken!

> My guess is something like this:
>
> (defun unifiable-word-p (scrambled plain)
>    (loop with mappings = (make-hash-table)
>          for s across scrambled
>          for p across plain
>          for v = (gethash c mappings) ;; where does c come from?!?
>          when (magic-fails v s p)
>          return nil
>          finally (return t)))
>
Good guess, and s/c/s/ in gethash (sorry about the typo).  Thanks!

Now, magic-fails is actually a let form with a when inside, but even
without testing it, I presume I can just plug that in instead of magic-
fails, right?

Could you, or someone else, explain why I can't use do (when ...) for
returning here?  Should I have used loop-finish?  Or better yet, what
is do typically used for, what can it be used for, and what can it in
particular *not* be used for?

LOOP is a strange creature, indeed...

--
Mikael Jansson
http://mikael.jansson.be
From: Pascal Bourguignon
Subject: Re: LOOP probblems
Date: 
Message-ID: <87y77ri74g.fsf@thalassa.informatimago.com>
Mikael Jansson <··············@gmail.com> writes:

> On 6 Apr, 01:10, Pascal Costanza <····@p-cos.net> wrote:
>> Mikael Jansson wrote:
>>
>> > Obviously I'm
>> > confused about the syntax of LOOP, but not even the "Tutorial for the
>> > Common Lisp Loop Macro" couldn't help me! �I get the feeling the
>> > things I'm doing with LET-bindings are built into LOOP already, but I
>> > don't know how to unleash its powers yet...
>>
>> > The code::
>>
>> >   (defun unifiable-words-p (scrambled plain)
>> >     (let ((mappings (make-hash-table)))
>> >       (loop for s across scrambled
>> >             for p across plain
>> >             do (let ((v (gethash c 'mappings)))
>> >                  (when (magic-fails v s p))
>> >                    return nil))
>> >             ;; otherwise(?)
>> >             return t)))
>>
>> You don't refer to lexical variables, like mappings, by quoting them in
>> references. Don't say (gethash c 'mappings), just say (gethash c mappings).
>>
> I don't understand. Are you saying I should never quote the hash
> parameter to gethash? I just remember it being quoted last time I
> looked at gethash, but obviously I was mistaken!
>
>> My guess is something like this:
>>
>> (defun unifiable-word-p (scrambled plain)
>>    (loop with mappings = (make-hash-table)
>>          for s across scrambled
>>          for p across plain
>>          for v = (gethash c mappings) ;; where does c come from?!?
>>          when (magic-fails v s p)
>>          return nil
>>          finally (return t)))
>>
> Good guess, and s/c/s/ in gethash (sorry about the typo).  Thanks!
>
> Now, magic-fails is actually a let form with a when inside, but even
> without testing it, I presume I can just plug that in instead of magic-
> fails, right?
>
> Could you, or someone else, explain why I can't use do (when ...) for
> returning here?

You could.  But using the RETURN operator, not the RETURN symbol,
which would be an error there, since when doesn't put its body in a
TAGBODY.

  (when (magic-fails v s p)
     (return nil))


> Should I have used loop-finish?  Or better yet, what
> is do typically used for, what can it be used for, and what can it in
> particular *not* be used for?
>
> LOOP is a strange creature, indeed...

 (defun unifiable-word-p (scrambled plain)
    (loop with mappings = (make-hash-table)
          for s across scrambled
          for p across plain
          for v = (gethash s mappings)
          never (magic-fails v s p)))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.
From: Pascal Costanza
Subject: Re: LOOP probblems
Date: 
Message-ID: <65rqlmF2g9uubU1@mid.individual.net>
Mikael Jansson wrote:
> On 6 Apr, 01:10, Pascal Costanza <····@p-cos.net> wrote:
>> Mikael Jansson wrote:
>>
>>> Obviously I'm
>>> confused about the syntax of LOOP, but not even the "Tutorial for the
>>> Common Lisp Loop Macro" couldn't help me!  I get the feeling the
>>> things I'm doing with LET-bindings are built into LOOP already, but I
>>> don't know how to unleash its powers yet...
>>> The code::
>>>   (defun unifiable-words-p (scrambled plain)
>>>     (let ((mappings (make-hash-table)))
>>>       (loop for s across scrambled
>>>             for p across plain
>>>             do (let ((v (gethash c 'mappings)))
>>>                  (when (magic-fails v s p))
>>>                    return nil))
>>>             ;; otherwise(?)
>>>             return t)))
>> You don't refer to lexical variables, like mappings, by quoting them in
>> references. Don't say (gethash c 'mappings), just say (gethash c mappings).
>>
> I don't understand. Are you saying I should never quote the hash
> parameter to gethash? I just remember it being quoted last time I
> looked at gethash, but obviously I was mistaken!

No, I'm saying that you should understand the constructs that you're 
using. ;)

The second parameter to gethash should refer to a hashtable, not to a 
symbol. A quote always returns whatever is quoted, without evaluating 
it. 'mappings evaluates to the symbol mappings. Why do you expect that 
to be a valid hashtable?

>> My guess is something like this:
>>
>> (defun unifiable-word-p (scrambled plain)
>>    (loop with mappings = (make-hash-table)
>>          for s across scrambled
>>          for p across plain
>>          for v = (gethash c mappings) ;; where does c come from?!?
>>          when (magic-fails v s p)
>>          return nil
>>          finally (return t)))
>>
> Good guess, and s/c/s/ in gethash (sorry about the typo).  Thanks!
> 
> Now, magic-fails is actually a let form with a when inside, but even
> without testing it, I presume I can just plug that in instead of magic-
> fails, right?
> 
> Could you, or someone else, explain why I can't use do (when ...) for
> returning here?  Should I have used loop-finish?  Or better yet, what
> is do typically used for, what can it be used for, and what can it in
> particular *not* be used for?

Don't guess. Pick the chapter "LOOP for black belts" in Practical Common 
Lisp, and learn.


Pascal

-- 
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Mikael Jansson
Subject: Re: LOOP probblems
Date: 
Message-ID: <d7a5383d-7937-4528-a756-886023c83161@z38g2000hsc.googlegroups.com>
On 6 Apr, 13:32, Pascal Costanza <····@p-cos.net> wrote:
> Mikael Jansson wrote:
> > On 6 Apr, 01:10, Pascal Costanza <····@p-cos.net> wrote:

> >> You don't refer to lexical variables, like mappings, by quoting them in
> >> references. Don't say (gethash c 'mappings), just say (gethash c mappings).
>
> > I don't understand. Are you saying I should never quote the hash
> > parameter to gethash? I just remember it being quoted last time I
> > looked at gethash, but obviously I was mistaken!
>
> No, I'm saying that you should understand the constructs that you're
> using. ;)
>
Well, I understand that QUOTE returns its first parameter, which is
used to circumvent the standard rules of function application, where
all arguments are evaluated before they're sent in, in the end getting
the symbol *name* instead of value.

However, I once saw this code, (gethash key 'table), thought it was
odd but didn't think of it more after; "Weird. But then again, there's
probably a good reason for that," -- I guess I should've checked it
up, instead of accepting something which obviously wasn't correct,
saving your time in the process :)

> The second parameter to gethash should refer to a hashtable, not to a
> symbol. A quote always returns whatever is quoted, without evaluating
> it. 'mappings evaluates to the symbol mappings. Why do you expect that
> to be a valid hashtable?

#include <std/handwaving>

>
> > Could you, or someone else, explain why I can't use do (when ...) for
> > returning here?  Should I have used loop-finish?  Or better yet, what
> > is do typically used for, what can it be used for, and what can it in
> > particular *not* be used for?
>
> Don't guess. Pick the chapter "LOOP for black belts" in Practical Common
> Lisp, and learn.

I actually thought about the book, recalling I left it at home. Now,
when you told me, I suddenly remember that it is available online...

Brain no worky today.

Thanks a bunch for your patience with me!

--
Mikael Jansson
http://mikael.jansson.be
From: Pascal Bourguignon
Subject: Re: LOOP probblems
Date: 
Message-ID: <87tzifhzq4.fsf@thalassa.informatimago.com>
Mikael Jansson <··············@gmail.com> writes:

> On 6 Apr, 13:32, Pascal Costanza <····@p-cos.net> wrote:
>> Mikael Jansson wrote:
>> > On 6 Apr, 01:10, Pascal Costanza <····@p-cos.net> wrote:
>
>> >> You don't refer to lexical variables, like mappings, by quoting them in
>> >> references. Don't say (gethash c 'mappings), just say (gethash c mappings).
>>
>> > I don't understand. Are you saying I should never quote the hash
>> > parameter to gethash? I just remember it being quoted last time I
>> > looked at gethash, but obviously I was mistaken!
>>
>> No, I'm saying that you should understand the constructs that you're
>> using. ;)
>>
> Well, I understand that QUOTE returns its first parameter, which is
> used to circumvent the standard rules of function application, where
> all arguments are evaluated before they're sent in, in the end getting
> the symbol *name* instead of value.

Well, no. 

The result of (QUOTE S) is not the name of the symbol S, it's the
symbol S itself.

What would return the name of the symbol S is (SYMBOL-NAME (QUOTE S))
and this would be the string "S", not the symbol S.


QUOTE doesn't circumvent the standard rules of function application.
In (SYMBOL-NAME (QUOTE S)), the argument to the function SYMBOL-NAME
is still evalated, like any other argument to any other function.

The evaluation of this argument, (QUOTE S), consists, since QUOTE is a
special form with special rules, in returning S itself with no
evaluation.  But to do that, (QUOTE S) must be evaluated, and it is,
so we get to call the function SYMBOL-NAME with the symbol S as
argument.


> However, I once saw this code, (gethash key 'table), thought it was
> odd but didn't think of it more after; "Weird. But then again, there's
> probably a good reason for that," -- I guess I should've checked it
> up, instead of accepting something which obviously wasn't correct,
> saving your time in the process :)

Yes, obviously:


C/USER1[99]> (let ((key 'k)) (gethash key 'table))

*** - GETHASH: argument TABLE is not a hash table



>> The second parameter to gethash should refer to a hashtable, not to a
>> symbol. A quote always returns whatever is quoted, without evaluating
>> it. 'mappings evaluates to the symbol mappings. Why do you expect that
>> to be a valid hashtable?
>
> #include <std/handwaving>

Not here.

Perhaps (use-package :DWIM), but then you'll have to re-implement that
package yourself, it's not standard.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: Thomas A. Russ
Subject: Re: LOOP probblems
Date: 
Message-ID: <ymi4pad1ub5.fsf@blackcat.isi.edu>
Pascal Bourguignon <···@informatimago.com> writes:

> Mikael Jansson <··············@gmail.com> writes:

> >
> > #include <std/handwaving>
> 
> Not here.
> 
> Perhaps (use-package :DWIM), but then you'll have to re-implement that
> package yourself, it's not standard.

Or would that have to be
       (require dwim)
    or (asdf:operate 'asdf:load-op 'dwim)
    or (mk:oos :dwim :load)

Just to tie this in to the standard package/module confusion thread.  ;-)

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Alex Mizrahi
Subject: Re: LOOP probblems
Date: 
Message-ID: <47f8ad91$0$90264$14726298@news.sunsite.dk>
 ??>> You don't refer to lexical variables, like mappings, by quoting them
 ??>> in references. Don't say (gethash c 'mappings), just say (gethash c
 ??>> mappings).

 MJ> I don't understand. Are you saying I should never quote the hash
 MJ> parameter to gethash? I just remember it being quoted last time I
 MJ> looked at gethash, but obviously I was mistaken!

you should never use quote unless you understand what it does. (quote 
mappings) returns a SYMBOL named "MAPPINGS".
GETHASH function accepts hash-table as parameter. so this won't work.

 MJ> Could you, or someone else, explain why I can't use do (when ...) for
 MJ> returning here?

you can, but you should use lisp form that returns, not special loop 
constuct "return nil" -- they work only at "top level" of loop, not inside 
other forms.
(return nil) would work though.

 MJ>   Should I have used loop-finish?

this will work too.

 MJ>   Or better yet, what is do typically used for, what can it be used
 MJ> for, and what can it in particular *not* be used for?

it's used to do something for side effects.
if something returns a value, you don't need DO.

you can write your code much easier if you learn more LOOP examples:

 (defun unifiable-words-p (scrambled plain)
      (loop with mappings = (make-hash-table)
                for s across scrambled
                for p across plain
                for v = (gethash s mappings)
                never (magic-fails v s p)))

ain't this much more readable? 
From: Juho Snellman
Subject: Re: LOOP probblems
Date: 
Message-ID: <87ve2vhpbv.fsf@vasara.proghammer.com>
Mikael Jansson <··············@gmail.com> writes:

> Trying to get the following code to work, but SBCL tells me it has
> deleted unreachable code, namely the GETHASH stuff. Obviously I'm
> confused about the syntax of LOOP, but not even the "Tutorial for the
> Common Lisp Loop Macro" couldn't help me!  I get the feeling the
> things I'm doing with LET-bindings are built into LOOP already, but I
> don't know how to unleash its powers yet...
> 
> The code::
> 
>   (defun unifiable-words-p (scrambled plain)
>     (let ((mappings (make-hash-table)))
>       (loop for s across scrambled
>             for p across plain
>             do (let ((v (gethash c 'mappings)))
>                  (when (magic-fails v s p))
>                    return nil))
>             ;; otherwise(?)
>             return t)))
> 
> Any clues on how to achieve what I'm trying to do? Sorry for not
> providing more detail; the code is from memory (no 'net access from
> the code computer).

Not giving actual code, but some retyped version of it that, makes
this question pointless. There are at least 4 obvious errors in this
reconstruction of the code. It's impossible for anyone else to say
which of those were present in your original code. It could even be
that the real problem is no longer present in this version.

-- 
Juho Snellman