[plt-scheme] no args with case-lambda
Use
(case-lambda
[() 'no-arguments]
[(arg1) 'one-argument]
[(arg1 arg2) 'two-arguments])
ifconfig
BAGOS
http://bagos.sourceforge.net
----- Original Message -----
From: "David J. Neu" <djneu at att.net>
To: <plt-scheme at list.cs.brown.edu>
Sent: Friday, February 06, 2004 5:54 PM
Subject: [plt-scheme] no args with case-lambda
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Hi all,
>
> I have a function that I'd like to accept, 0, 1 or 2 arguments.
>
> Is there a way to have case-lambda match the 0 args case explicitly or
> must I match to a list, check if the list is null? and then raise an
> exn:application:arity if it is?
>
> I show an example below where I raise the exception.
>
> Many thanks for any help!
>
> --David
>
>
> (require (lib "date.ss" "mzlib"))
>
> ; (current-date-time) => 2004-02-06 09:39:40
> ; (current-date-time #f) => 2004-02-06
> ; (current-date-time #t 'american) => Friday, February 6th, 2004 9:39:40am
> (define current-date-time
> (let ((cdt (lambda (atime aformat)
> (let ((ld #f))
> (date-display-format aformat)
> (set! ld (date->string (seconds->date (current-seconds))
atime))
> (date-display-format 'american)
> ld))))
> (case-lambda
> ((ltime) (cdt ltime 'iso-8601))
> ((ltime lformat) (cdt ltime lformat))
> ;(lls (cdt #t 'iso-8601)))))
> ; Q: what if lls is not '()
> (lls (if (null? lls)
> (cdt #t 'iso-8601)
> (let ((llen (length lls)))
> (raise (make-exn:application:arity
> (format "current-date-time: expects 0,1 or 2
arguments, given ~a" llen)
> (current-continuation-marks)
> llen
> 0))))))))
>
>
>
> (display (current-date-time))
> (newline)
> (display (current-date-time #f))
> (newline)
> (display (current-date-time #t 'american))
> (newline)
> (with-handlers
> ((exn:application:arity?
> (lambda (e) (printf "got ~a args but wanted ~a!~n"
(exn:application-value e) (exn:application:arity-expected e)))))
> (display (current-date-time #t 'american 'junk)))
> (newline)
> (display (current-date-time #t 'american 'junk))
>