## Determine number of arguments for a function

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
White_Owl
Posts: 15
Joined: Wed Sep 13, 2017 2:49 pm

### Determine number of arguments for a function

I need to find out how many arguments a function expects.

Code: Select all

``````(args? #'eql) -> 2
(args? #'length) -> 1
(args? (lambda (a b c) (+ a b c))) ->3``````
Where to start?

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

### Re: Determine number of arguments for a function

What do you expect the result for these are?

Code: Select all

``````(lambda (a &rest rest) rest)

(lambda (a &optional b) b)

(lambda (a &key (a 4) b c) c)
``````
Also, the name args? seems like you are doing a predicate and like Scheme naming conventions.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

White_Owl
Posts: 15
Joined: Wed Sep 13, 2017 2:49 pm

### Re: Determine number of arguments for a function

Only the necessary arguments should be counted. Optional and rest can be ignored.

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

### Re: Determine number of arguments for a function

In Clozure CL:

Code: Select all

``````(defun required-parameters (function)
(loop for thing in (ccl:arglist function)
until (member thing lambda-list-keywords)
collect thing))``````

White_Owl
Posts: 15
Joined: Wed Sep 13, 2017 2:49 pm

### Re: Determine number of arguments for a function

Thank you.
That is a perfect solution.

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

### Re: Determine number of arguments for a function

No it's not.. It locks you in to one implementation. Thus you are no longer writing Common Lisp but CCLs superset of Common Lisp. It's very unfortunate!

BTW: Are you sure this is not a XY problem? What are you going to use this for?
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

White_Owl
Posts: 15
Joined: Wed Sep 13, 2017 2:49 pm

### Re: Determine number of arguments for a function

sylwester wrote:No it's not.. It locks you in to one implementation. Thus you are no longer writing Common Lisp but CCLs superset of Common Lisp. It's very unfortunate!
The only change required for making it a proper CLISP is to remove ccl prefix from arglist function

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

### Re: Determine number of arguments for a function

Proper CLISP??? CLISP is not a synonym for Common Lisp. CLISP is one of many Common Lisp implementations and just like SBCL or CCL. As with all implementations they might be a superset and sure enough arglist is defined in it, but it doesn't make it more portable.

Code: Select all

``````[1]> (defpackage :test (:use :cl))
#<package test>
[2]> (in-package :test)
#<package test>
test[3]> #'arglist

*** - function: undefined function arglist
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead of (fdefinition 'arglist).
RETRY          :R2      Retry
STORE-VALUE    :R3      Input a new value for (fdefinition 'arglist).
ABORT          :R4      Abort main loop
Break 1 test[4]>
``````
See?
arglist is the symbol arglist, lies in #<package ext>, is accessible in 6 packages COMMON-LISP-USER,
CS-COMMON-LISP-USER, EXT, POSIX, SCREEN, SYSTEM, names a function, has 1 property system::doc
Not portable! Not Common Lisp.

See this answer in SO
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

White_Owl
Posts: 15
Joined: Wed Sep 13, 2017 2:49 pm

### Re: Determine number of arguments for a function

sylwester wrote:Proper CLISP??? CLISP is not a synonym for Common Lisp. CLISP is one of many Common Lisp implementations and just like SBCL or CCL. As with all implementations they might be a superset and sure enough arglist is defined in it, but it doesn't make it more portable.

Code: Select all

``````[1]> (defpackage :test (:use :cl))
#<package test>
[2]> (in-package :test)
#<package test>
test[3]> #'arglist

*** - function: undefined function arglist
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead of (fdefinition 'arglist).
RETRY          :R2      Retry
STORE-VALUE    :R3      Input a new value for (fdefinition 'arglist).
ABORT          :R4      Abort main loop
Break 1 test[4]>
``````
See?
Sorry, what should I see? You forcibly switched to a different package, did not find a function in it, and conclude that the function is not defined in the implementation???
Then look here:

Code: Select all

``````C:dev>clisp -on-error abort
i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
I I I I I I I      8     8   8           8     8     o  8    8
I  \ `+' /  I      8         8           8     8        8    8
\  `-+-'  /       8         8           8      ooooo   8oooo
`-__|__-'        8         8           8           8  8
|            8     o   8           8     o     8  8
------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010

Type :h and hit Enter for context help.

[1]> #'arglist
#<COMPILED-FUNCTION ARGLIST>
[2]> (arglist #'eql)
(#:ARG0 #:ARG1)
[3]> (arglist #'length)
(#:ARG0)
[4]> (defpackage :test (:use :cl))
#<PACKAGE TEST>
[5]>  (in-package :test)
#<PACKAGE TEST>
TEST[6]>  #'arglist
*** - FUNCTION: undefined function ARGLIST

TEST[7]>                                                             ``````
See?