From: Kareem Nutt
Subject: Difficulty with funcall
Date: 
Message-ID: <yYSdnc-pLK5HdMncRVn-rw@comcast.com>
I'm having trouble with the following piece of code.  The problem (at 
least the immediate one) is that if funcName takes 3 arguments, then 
what i have does (funcall funcName (a b c)) instead of (funcall funcName 
a b c).

(mapcar (lambda (x) (if (eq (funcall funcName x) output) (matchResult x 
(makeRL labels args))))
                  (permutations labels)))

- funcName is passed in, so it's not known how many args it takes
- x is a list of args of the correct amount and order for funcName
- permutations returns a list of lists ((a b c) (c b a ) ...) based on a 
passed in list (labels)
- matchResult returns the solution based on the correct permutation (x) 
from the key/value list returned by makeRL

any ideas?  am i way off?  (is there more wrong than the funcall part?) 
  thanks.

kareem

From: Jeff
Subject: Re: Difficulty with funcall
Date: 
Message-ID: <8q65d.150822$3l3.86442@attbi_s03>
Kareem Nutt wrote:

> I'm having trouble with the following piece of code.  The problem (at
> least the immediate one) is that if funcName takes 3 arguments, then
> what i have does (funcall funcName (a b c)) instead of (funcall
> funcName a b c).

Use APPLY instead of FUNCALL. APPLY assumes a list of arguments instead
of them each being separated out.

Jeff

-- 
Progress in computer technology is waiting for people to stop
re-inventing the wheel.
 - Dr. William Bland (comp.lang.lisp)
From: Kalle Olavi Niemitalo
Subject: Re: Difficulty with funcall
Date: 
Message-ID: <873c16ewog.fsf@Astalo.kon.iki.fi>
Kareem Nutt <··········@gmail.com> writes:

> The problem (at least the immediate one) is that if funcName
> takes 3 arguments, then what i have does (funcall funcName (a b
> c)) instead of (funcall funcName a b c).

Use (apply funcName x) instead of (funcall funcName x).

> (is there more wrong than the funcall part?)

Possibly you should compare results with EQUAL or MISMATCH,
rather than EQ.  This would detect lists with similar elements
even if they are made of different cons cells.
From: Marco Antoniotti
Subject: Re: Difficulty with funcall
Date: 
Message-ID: <DDe5d.31$NQ1.8754@typhoon.nyu.edu>
You need APPLY.  The difference between FUNCALL and APPLY is - in 
essence - that FUNCALL must know the exact shape of the argument list of 
the function argument, while APPLY uses its last argument to pile up all 
the arguments to the function in a list.

I.e. given

(defun foo (a b c) #|...|#)

the following are equivalent

(funcall 'foo 1 2 3)
(apply 'foo '(1 2 3))
(apply 'foo 1 '(2 3))
(apply 'foo 1 2 '(3))
(apply 'foo 1 2 3 ())

APPLY and FUNCALL are both useful for a number of reasons, which this 
posting is too small to enumerate :)

Cheers

--
Marco


Kareem Nutt wrote:
> I'm having trouble with the following piece of code.  The problem (at 
> least the immediate one) is that if funcName takes 3 arguments, then 
> what i have does (funcall funcName (a b c)) instead of (funcall funcName 
> a b c).
> 
> (mapcar (lambda (x) (if (eq (funcall funcName x) output) (matchResult x 
> (makeRL labels args))))
>                  (permutations labels)))
> 
> - funcName is passed in, so it's not known how many args it takes
> - x is a list of args of the correct amount and order for funcName
> - permutations returns a list of lists ((a b c) (c b a ) ...) based on a 
> passed in list (labels)
> - matchResult returns the solution based on the correct permutation (x) 
> from the key/value list returned by makeRL
> 
> any ideas?  am i way off?  (is there more wrong than the funcall part?) 
>  thanks.
> 
> kareem
From: Kenny Tilton
Subject: Re: Difficulty with funcall
Date: 
Message-ID: <aNc5d.1505$mH1.2518153@twister.nyc.rr.com>
Kareem Nutt wrote:
> I'm having trouble with the following piece of code.  The problem (at 
> least the immediate one) is that if funcName takes 3 arguments, then 
> what i have does (funcall funcName (a b c)) instead of (funcall funcName 
> a b c).
> 
> (mapcar (lambda (x) (if (eq (funcall funcName x) output) (matchResult x 
> (makeRL labels args))))
>                  (permutations labels)))
> 
> - funcName is passed in, so it's not known how many args it takes
> - x is a list of args of the correct amount and order for funcName
> - permutations returns a list of lists ((a b c) (c b a ) ...) based on a 
> passed in list (labels)
> - matchResult returns the solution based on the correct permutation (x) 
> from the key/value list returned by makeRL
> 
> any ideas?  am i way off?  (is there more wrong than the funcall part?) 
>  thanks.

In addition to the notes already posted on APPLY and EQ:

- use WHEN if there is nothing to do on the false branch of an IF
- use hypenated names instead of camelcase to avoid the wrath of the 
Lisp Purification Squad

(mapcar (lambda (x)
           (when (equal (apply func-name x) output)
               (match-result x (make-rl labels args))))
   (permutations labels))

As a style point, I would also at least use "p" as the parameter name 
for the anonymous function. No need to spell out "permutation" for such 
a small body of code, but "x" obfuscates needlessly.

As for the original EQ, that is possibly worse than Kalle suggested. For 
example, EQ cannot be used reliably for numbers and characters because 
(roughly speaking) the compiler is free to optimize things such that EQ 
will not detect the similarity of 2 and 2.

(eql (list 1 2 3)(list 1 2 3)) => nil
(equal (list 1 2 3)(list 1 2 3)) => t
(let ((list (list 1 2 3)))
    (eq list list)) => t

Use EQUAL as those examples suggest when you want to compare the 
contents of lists. Otherwise, you can use EQ unless the values compared 
might be numbers or characters.

kenny


-- 
Cells? Cello? Celtik?: http://www.common-lisp.net/project/cells/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
From: David Sletten
Subject: Re: Difficulty with funcall
Date: 
Message-ID: <6jl5d.20588$OB2.14232@twister.socal.rr.com>
Kenny Tilton wrote:


> In addition to the notes already posted on APPLY and EQ:
> 
> - use WHEN if there is nothing to do on the false branch of an IF
> - use hypenated names instead of camelcase to avoid the wrath of the 
> Lisp Purification Squad
> 
> (mapcar (lambda (x)
>           (when (equal (apply func-name x) output)
>               (match-result x (make-rl labels args))))
>   (permutations labels))
> 

I agree with you on the use of WHEN as a general principle, but I 
wouldn't use it in this case since it's not true that nothing happens 
when the test fails. Since MAPCAR is involved the implicit NIL returned 
by WHEN will be used in the resulting list. I think it would be clearer 
here to explicitly show this using IF:
(mapcar #'(lambda (x) (if (equal ... ) (match-result ...) nil)...)

Off the top of my head it seems that the use of WHEN is appropriate when 
there is no _side_effect_ if the test is false. If the WHEN form is used 
to produce a value as part of a larger expression, then it has to return 
something whether or not the test passes.

Example:
(let ((min 8)
       (max 2))
   (when (< max min)
     (rotatef min max))
...)

David Sletten