From: Aaron Mueller
Subject: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <1186628767.107114.223910@l70g2000hse.googlegroups.com>
Hallo everyone!

I am new to lisp and make my first steps. I Read the eBook "A Gentle
Introduction to Symbolic Computation" (http://www.cs.cmu.edu/~dst/
LispBook/index.html) and I have now some problems with an excersise
(Page 163)

Here is my code:
(defun rule (thr n)
  (if (and
       (equal (first thr) n)
       (equal (second thr) n))
      t nil))

(rule (list '(1 1) '1))

The Emacs-Debug-Window outputs this error-message:
Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (thr
n) (if (and (equal (first thr) n) (equal (second thr) n)) t nil)) 1)
  rule(((1 1) 1))
  eval((rule (list (quote ...) (quote 1))))

My question: How can i pass a list as argument to a function? The way
i tryed seems wrong (if I understand the error-message right, lisp
passes the function "rule" instead the list)).

Greetings,
Aaron

From: Ken Tilton
Subject: Re: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <Wawui.260$hP5.182@newsfe12.lga>
Aaron Mueller wrote:
> Hallo everyone!
> 
> I am new to lisp and make my first steps.

Damn Ruby, damn RoR, and a curse on Matz.

> I Read the eBook "A Gentle
> Introduction to Symbolic Computation" (http://www.cs.cmu.edu/~dst/
> LispBook/index.html) and I have now some problems with an excersise
> (Page 163)
> 
> Here is my code:
> (defun rule (thr n)
>   (if (and
>        (equal (first thr) n)
>        (equal (second thr) n))
>       t nil))

(if x t nil) may as well be just x, since any non-nil value will be 
treated as true, and good Lisp code generally tests for non-nil, not the 
precise value t.

> 
> (rule (list '(1 1) '1))
> 
> The Emacs-Debug-Window outputs this error-message:
> Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (thr
> n) (if (and (equal (first thr) n) (equal (second thr) n)) t nil)) 1)
>   rule(((1 1) 1))
>   eval((rule (list (quote ...) (quote 1))))
> 
> My question: How can i pass a list as argument to a function? The way
> i tryed seems wrong (if I understand the error-message right, lisp
> passes the function "rule" instead the list)).

Your function has two parameters, thr and n. How many are you supplying 
with (list '(1 1) '1)? And why are you quoting 1?

kt
From: Rainer Joswig
Subject: Re: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <joswig-B86D32.11100209082007@news-europe.giganews.com>
In article <························@l70g2000hse.googlegroups.com>,
 Aaron Mueller <·······@gmail.com> wrote:

> Hallo everyone!
> 
> I am new to lisp and make my first steps. I Read the eBook "A Gentle
> Introduction to Symbolic Computation" (http://www.cs.cmu.edu/~dst/
> LispBook/index.html) and I have now some problems with an excersise
> (Page 163)
> 
> Here is my code:
> (defun rule (thr n)
>   (if (and
>        (equal (first thr) n)
>        (equal (second thr) n))
>       t nil))

Expressions return values. The IF is not needed.

> 
> (rule (list '(1 1) '1))
> 
> The Emacs-Debug-Window outputs this error-message:

But you are running Common Lisp from Emacs, right?

> Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (thr
> n) (if (and (equal (first thr) n) (equal (second thr) n)) t nil)) 1)
>   rule(((1 1) 1))
>   eval((rule (list (quote ...) (quote 1))))
> 
> My question: How can i pass a list as argument to a function? The way
> i tryed seems wrong (if I understand the error-message right, lisp
> passes the function "rule" instead the list)).

Your function RULE has two arguments THR and N.
You call the function RULE with only one argument, a list.

You want to call RULE with two arguments:

(rule '(1 1) 1)

If you already have a list like '((1 1) 1) and you want
to apply RULE to it, the use, well, APPLY.

(apply #'rule '((1 1) 1))



> 
> Greetings,
> Aaron

-- 
http://lispm.dyndns.org
From: Aaron Mueller
Subject: Re: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <1186686184.509391.212700@j4g2000prf.googlegroups.com>
On Aug 9, 11:10 am, Rainer Joswig <······@lisp.de> wrote:

> Damn Ruby, damn RoR, and a curse on Matz.

Its refreshing to learn a new language, even through this language is
not *bling* Web2.0 hyped *bling* :)

> Expressions return values. The IF is not needed.

Thanks!

> > The Emacs-Debug-Window outputs this error-message:
>
> But you are running Common Lisp from Emacs, right?

I use now LispWorks.

> Your function RULE has two arguments THR and N.
> You call the function RULE with only one argument, a list.

Oh, to much parenthesis :)
For your amusement, here my solution. If you see some bad mistakes
oder things which could be better, let me know.


(defun dice nil
  (1+ (random 6)))

(defun throw-dice nil
  (list (dice) (dice)))

(defun rule (thr n)
  (and (eql (first thr) n)
       (eql (second thr) n)))

(defun sum-throw (thr)
  (+ (first thr) (second thr)))

(defun snake-eyes-p (thr) (rule thr 1))
(defun boxcars-p (thr) (rule thr 6))

(defun instant-win-p (thr)
  (or (eql (sum-throw thr) 7)
      (eql (sum-throw thr) 11)))

(defun instant-loss-p (thr)
    (or (eql (sum-throw thr) 2)
        (eql (sum-throw thr) 3)
        (eql (sum-throw thr) 12)))

(defun say-throw (thr)
  (cond ((snake-eyes-p thr) 'SNAKE-EYES)
        ((boxcars-p thr) 'BOXCARS)
        (t (sum-throw thr))))

(defun craps nil
  (let ((thr (throw-dice)))
    (append (list 'THROW (first thr)
                  'AND (second thr)
                  '-- (say-throw thr) '--)
            (cond ((instant-win-p thr) '(YOU WIN))
                  ((instant-loss-p thr) '(YOU LOSE))
                  (t (list 'YOUR 'POINT 'IS (sum-throw thr)))))))

Cya,
Aaron
From: Thomas A. Russ
Subject: Re: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <ymiy7gk4acx.fsf@blackcat.isi.edu>
Aaron Mueller <·······@gmail.com> writes:

> For your amusement, here my solution. If you see some bad mistakes
> oder things which could be better, let me know.
> 
> 
> (defun dice nil
>   (1+ (random 6)))

Style issue.  Generally argument lists that are empty are written as
"()" instead of "nil", since it makes the list nature clearer.  These
are synonyms in the language, but it just looks weird to me -- with 25+
years lisp experience -- not to have parentheses for the argument list.
Better would be:

  (defun dice ()
    (1+ (random 6)))

An even better name would be something like "die-roll", since that is
what is being done.


> (defun throw-dice nil
>   (list (dice) (dice)))
> 
> (defun rule (thr n)
>   (and (eql (first thr) n)
>        (eql (second thr) n)))

I would pick a better name for this.  Perhaps reversing the argument
order and calling it DOUBLE-N-P instead, since it checks to see if the
dice roll is a double matching a particular number.

> (defun sum-throw (thr)
>   (+ (first thr) (second thr)))

As you get more Lisp experience, you'll find a cool way to make this a
more general function that will allow it to work with any number of
dice.


> (defun snake-eyes-p (thr) (rule thr 1))
> (defun boxcars-p (thr) (rule thr 6))
> 
> (defun instant-win-p (thr)
>   (or (eql (sum-throw thr) 7)
>       (eql (sum-throw thr) 11)))

It is a bit wasteful to compute the dice sum twice.  Adding a local
variable can eliminate that:

(defun instant-win-p (throw)
   (let ((sum (sum-throw throw)))
      (or (eql thr 7)
          (eql thr 11))))


> (defun instant-loss-p (thr)
>     (or (eql (sum-throw thr) 2)
>         (eql (sum-throw thr) 3)
>         (eql (sum-throw thr) 12)))

Or, alternately, you could use another neat lisp function to test for
matches to a set of answers:

(defun instant-loss-p (throw)
  (member (sum-throw throw) '(2 3 12)))

The FIND function could also be used.


The following all do some sort of output.  There are some nice printing
and formating routines available as well.  I expect you will encouter
them as your lisp learning continues.

> (defun say-throw (thr)
>   (cond ((snake-eyes-p thr) 'SNAKE-EYES)
>         ((boxcars-p thr) 'BOXCARS)
>         (t (sum-throw thr))))
> 
> (defun craps nil
>   (let ((thr (throw-dice)))
>     (append (list 'THROW (first thr)
>                   'AND (second thr)
>                   '-- (say-throw thr) '--)

I would fold this entire first LIST production into the SAY-THROW
function, since this all seems to be related to how you want to express
the dice roll result.

>             (cond ((instant-win-p thr) '(YOU WIN))
>                   ((instant-loss-p thr) '(YOU LOSE))
>                   (t (list 'YOUR 'POINT 'IS (sum-throw thr)))))))
> 



-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Rainer Joswig
Subject: Re: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <joswig-97A355.00033310082007@news-europe.giganews.com>
In article <························@j4g2000prf.googlegroups.com>,
 Aaron Mueller <·······@gmail.com> wrote:

> On Aug 9, 11:10 am, Rainer Joswig <······@lisp.de> wrote:
> 
> > Damn Ruby, damn RoR, and a curse on Matz.
> 
> Its refreshing to learn a new language, even through this language is
> not *bling* Web2.0 hyped *bling* :)
> 
> > Expressions return values. The IF is not needed.
> 
> Thanks!
> 
> > > The Emacs-Debug-Window outputs this error-message:
> >
> > But you are running Common Lisp from Emacs, right?
> 
> I use now LispWorks.
> 
> > Your function RULE has two arguments THR and N.
> > You call the function RULE with only one argument, a list.
> 
> Oh, to much parenthesis :)
> For your amusement, here my solution. If you see some bad mistakes
> oder things which could be better, let me know.
> 
> 
> (defun dice nil
>   (1+ (random 6)))
> 
> (defun throw-dice nil
>   (list (dice) (dice)))

You usually don't write NIL if you mean an empty list.

(defun throw-dice ()
  (list (dice) (dice)))


> 
> (defun rule (thr n)
>   (and (eql (first thr) n)
>        (eql (second thr) n)))

If you compare numbers, I'd just use = :

(defun rule (thr n)
  (= (first thr) (second thr) n))

> 
> (defun sum-throw (thr)
>   (+ (first thr) (second thr)))
> 
> (defun snake-eyes-p (thr) (rule thr 1))
> (defun boxcars-p (thr) (rule thr 6))
> 
> (defun instant-win-p (thr)
>   (or (eql (sum-throw thr) 7)
>       (eql (sum-throw thr) 11)))
> 
> (defun instant-loss-p (thr)
>     (or (eql (sum-throw thr) 2)
>         (eql (sum-throw thr) 3)
>         (eql (sum-throw thr) 12)))


(member (sum-throw thr) '(2 3 12))         


> 
> (defun say-throw (thr)
>   (cond ((snake-eyes-p thr) 'SNAKE-EYES)
>         ((boxcars-p thr) 'BOXCARS)
>         (t (sum-throw thr))))
> 
> (defun craps nil
>   (let ((thr (throw-dice)))
>     (append (list 'THROW (first thr)
>                   'AND (second thr)
>                   '-- (say-throw thr) '--)
>             (cond ((instant-win-p thr) '(YOU WIN))
>                   ((instant-loss-p thr) '(YOU LOSE))
>                   (t (list 'YOUR 'POINT 'IS (sum-throw thr)))))))

Or


`(throw ,(first thr) and ,(second thr) -- ,(say-throw thr) --
  ,@(cond ((instant-win-p thr) '(YOU WIN))
          ((instant-loss-p thr) '(YOU LOSE))
          (t `(YOUR POINT IS ,(sum-throw thr)))))


Typically I would replace for anything more interesting
the list with a DEFCLASS or DEFSTRUCT

(defstruct the-throw one two)

Later you want to add special printing, more slots,
etc. Then this comes handy.




> 
> Cya,
> Aaron

-- 
http://lispm.dyndns.org
From: Richard Szopa
Subject: Re: Wrong number of Arguments with list-parameter
Date: 
Message-ID: <1186733427.059086.325660@d30g2000prg.googlegroups.com>
On Aug 9, 5:06 am, Aaron Mueller <·······@gmail.com> wrote:

> I am new to lisp and make my first steps. I Read the eBook "A Gentle
> Introduction to Symbolic Computation" (http://www.cs.cmu.edu/~dst/
> LispBook/index.html) and I have now some problems with an excersise
> (Page 163)
>
> Here is my code:
> (defun rule (thr n)
>   (if (and
>        (equal (first thr) n)
>        (equal (second thr) n))
>       t nil))
>
> (rule (list '(1 1) '1))
>
> The Emacs-Debug-Window outputs this error-message:
> Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (thr
> n) (if (and (equal (first thr) n) (equal (second thr) n)) t nil)) 1)
>   rule(((1 1) 1))
>   eval((rule (list (quote ...) (quote 1))))
>
> My question: How can i pass a list as argument to a function? The way
> i tryed seems wrong (if I understand the error-message right, lisp
> passes the function "rule" instead the list)).

You are passing your function one argument: a list with '(1 1) as its
first element, and 1 as its second element. Your function expects /
two/ arguments, thr and n. Try
(rule '(1 1) 1)

Alternatively (if that's what you want) you can use APPLY:
(apply #'rule (list '(1 1) 1))

BTW the IF in you function definition is unnecessary: the bare
conjunction is enough.

 (defun rule (thr n)
   (and
    (equal (first thr) n)
    (equal (second thr) n)))

Cheers,

    -- Richard