Re: keyword arguments and #:rest
Bonjour! 2012/11/10 Ludovic Courtès : > Hi, >> So I thought that maybe there could be another keyword controlling whether >> the keywords are left in the rest list or not, so the above code could >> look like this: >> >> (define* (random-array #:key (range 1.0) (type #t) (mean 0) #:rest >> dims #:no-key) >> (array-map (lambda (mean) (+ mean (- (random (* 2 range)) range))) >> (apply make-typed-array type mean dims))) >> >> Wouldn't the world be a better place? > > Perhaps, although that would make the ‘lambda*’ semantics yet more complex. That's true, but it's only in regard that is already mentioned in the documentation (and has to be, if we want to avoid confusion). So I think that in a way this would be a natural extension > However, in your example, what about using a list instead of a rest > argument for ‘dims’? That would also be a solution, but the way it is done now inherits the interface from make-typed-array, which is one thing less to memoize Best regards, Panicz
case-lambda* question
(define f (case-lambda* ((a b c #:key x) 3) ((a #:key x) 1))) scheme@(guile-user)> (g 0 #:x 1) $1 = 3 The manual says > Also, for completeness. Guile defines case-lambda* as well, which is like > case-lambda, except with lambda* clauses. A case-lambda* clause matches if > the arguments fill the required arguments, but are not too many for the > optional and/or rest arguments. > > Keyword arguments are possible with case-lambda*, but they do not contribute > to the “matching” behavior. That is to say, case-lambda* matches only on > required, optional, and rest arguments, and on the predicate; keyword > arguments may be present but do not contribute to the “success” of a match. > In fact a bad keyword argument list may cause an error to be raised. I see that it gives 3 because ‘a b c’ matches ‘0 #:x 1’, so it's counting #:x as an ‘argument’ by itself. This is not what I expected from the description above. I think that the description should make clear that each item in the arg list is counted as an argument for the purposes of matching. Moreover I think this behavior makes case-lambda* fairly useless, or even treacherous. I wonder what other people think. Regards, Daniel
Re: case-lambda* question
On 12 November 2012 21:54, Daniel Llorens wrote: > > (define f > (case-lambda* > ((a b c #:key x) 3) > ((a #:key x) 1))) > > scheme@(guile-user)> (g 0 #:x 1) > $1 = 3 Because “0 #:x 1” is a valid match for “a b c”, you should rearrange the case-lambda clauses. When the doc. states keyword arguments do not contribute to the success of a match, it refers only to keyword arguments in the case-lambda clause, not at the call site. This makes sense, otherwise it would inhibit writing functions that detect keywords internally from their rest arguments. scheme@(guile-user)> (define g (case-lambda* ((a #:key x) 1) ((a b c #:key x) 3))) scheme@(guile-user)> (g 0 #:x 1) $2 = 1 However, trying to call with three arguments then triggers an error, and I am not sure why: scheme@(guile-user)> (g 1 2 3) :46:1: In procedure g: :46:1: In procedure #: Invalid keyword Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guile-user) [1]> ,bt In current input: 46:1 0 (g 1 # 2 3) Definitely you need to rearrange the clauses. About this error, I don't know! Regards
I/O, modules
Hello, Could you show me some trivial programs related to I/O (e.g. read from file, convert to uppercase, write to another file)? This page [0] doesn't list a function that can be used to read the whole file at once. Is there a "read-string" function? What about "read-port"? Should I import some module to get the above functions? Which one? BTW, I have a problem with modules. How to check what can be imported? For example: I created two files in the same dir: test-scm.scm: (define-module (test-scm) #:export (test-func)) (define (test-func x) (+ x 1)) import-test.scm: (define-module (import-test) #:use-module (test-scm)) (display (test-func 2)) Why does "guile import-test.scm" raise "ERROR: no code for module (test-scm)"? [0] https://gnu.org/software/guile/manual/guile.html#Reading