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

Re: [Scheme-reports] values



It's clear to me that I can build a macro for this and make my life easier. However, I still need to call the macro. And if I don't I get an error. I really wish I could use functions multiple values also in contexts where one value is required, without even needing to know that the function returns more values. 

The most classic example is the integer division: the function's purpose is to return the integer which results by dividing two numbers. But it can also return the rest (modulo) of the division, as a "secondary" return value. It makes perfect sense since the modulo is already known, without any additional computation. But the purpose of the function remains the same: it will give you the division result. The modulo is just a by-product which happens to exist and therefore is also offered to the caller, which may choose to use it or not. Because it's secondary it will be ignored silently when the function is used in a single-value context. The caller could not event know that there are two returned values.

This philosophy of returning more than needed is present throughout lisp. I find it very good. It contributes to the shortness of the code.

I can also imagine situations where I would like to enhance a function of mine, by adding an extra return value, but not having to change the code where the function is used, because the semantic of the function has not changed. It is merely an enhancement. I must admit though, that such situation are not likely to occur often.

Disadvantage 1: By making the first value more important than others, you are enforcing a semantic, and everyone using multiple values will have to obey. But then you can always use lists, which do make sense. Say, you have two return values of equal importance (and it should be an error not to bind them when calling the function) and several by-products (which will be ignored silently if not bound). In this case, the first return value would be a list of two, and subsequent values would be my by-products.

Disadvantage 2: The syntax check is not so strict anymore. We all know where this leads. But I would happily trade it for the number of chars it saves me from typing (and of course the cleaness it adds to the code).

Razvan



From: John Cowan <cowan@x>
To: Razvan Rotaru <roti_343@x>
Cc: "scheme-reports@x" <scheme-reports@x>
Sent: Friday, May 27, 2011 5:56 PM
Subject: Re: [Scheme-reports] values

Razvan Rotaru scripsit:

> The truth is most of the time I'm interested in one value, not all
> of them. In my oppinion Lisp is right when assuming that one of the
> values returned as result is more important than others, and does not
> raise an error when I use such a function in a context where only one
> result is expected.

Use this trivial wrapper:

(define-syntax strip-values
  (syntax-rules ()
    ((strip-values exp)
    (call-with-values
      (lambda () exp)
      (lambda x (if (null? x) 'bogus (car x)))))))

> Additionally, in the way scheme treats multiple values, there is
> little difference between returning a list with the values, and
> returning the values as such (using the (values ...) form). I could
> very well use the list, and would not need to have the (values ...)
> form built into the language.

You could.  However, there may be a difference in performance.

--
An observable characteristic is not necessarily        John Cowan
a functional requirement.  --John Hudson                cowan@x


_______________________________________________
Scheme-reports mailing list
Scheme-reports@x
http://lists.scheme-reports.org/cgi-bin/mailman/listinfo/scheme-reports