guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scheme 102


From: Alejandro Forero Cuervo
Subject: Re: Scheme 102
Date: Wed, 28 Nov 2001 19:13:11 -0500
User-agent: Mutt/1.2.5i

Hey.

    say you read a function name + parameters from stdin or from user
    input.  how can i check the number of required parameters have
    been supplied without calling the function. this would avoid
    aborting when 2 param were supplied instead of 3 e.g
    
    ...read "f 1 2 3" from the command line
    ...fname is "f"
    ...list-of-params is "1 2 3"
    
    (define (f x y) (+ x y))
    ((eval (string->symbol fname)) list-of-params)
    ERROR: Wrong number of arguments to #<primitive-procedure f>
    
    any idea ?

Others have already explained how to know the number of parameters a
given procedure accepts.

I'm not sure this is what you want (what I'm going to explain may be
completely irrelevant to you), but you could make your procedures
accept an unlimited number of parameters and then handle weird things
inside of them.

For example, your version of "f" could be improved to accept an
unlimited number of parameters as in:

    (define (f . args) (apply + args))

If you then execute (f a1 a2 a3 ... an), f will receive all its arguments
inside of the list args.

You could, in the function, explicitly check the number of parameters
and display an error message (or do whatever is relevante, perhaps
return 0 or #f or ... whatever) when you detect something wrong, as
in:

    (define (f . args)
      (if (not (equal? (length args) 2))
        (display "Format: (f x y)")
        (+ (car args) (cadr args))))

If your function has required and optional parameters, you could do
something like:

    (define (f first-arg second-arg third-arg . rest) .....)

The function will receive the first parameter in first-arg, the second
parameter in second-arg, the third parameter in third-arg and whatever
comes next in the list rest.

Hope it works... as I said, it may be completely irrelevant to what
you want to do, I don't know.

Alejo.
http://bachue.com/alejo

--
The mere formulation of a problem is far more essential than its solution.
      -- Albert Einstein.

$0='!/sfldbi!yjoV0msfQ!sfiupob!utvK'x44;print map{("\e[7m \e[0m",chr ord
(chop$0)-1)[$_].("\n")[++$i%77]}split//,unpack'B*',pack'H*',($F='F'x19).
"F0F3E0607879CC1E0F0F339F3FF399C666733333CCF87F99E6133999999E67CFFCCF3".
"219CC1CCC033E7E660198CCE4E66798303873CCE60F3387$F"#Don't you love Perl?

Attachment: pgpPkCRqk2jFL.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]