* return value of `string-set!'
@ 2013-08-14 22:33 Alexandru Cojocaru
2013-08-15 5:29 ` William ML Leslie
2013-08-15 20:22 ` Taylan Ulrich B.
0 siblings, 2 replies; 4+ messages in thread
From: Alexandru Cojocaru @ 2013-08-14 22:33 UTC (permalink / raw)
To: guile-devel
Hi,
currently `string-set!' returns an unspecified value. What about making
it return `str' instead?
Here is an example of its use:
; make a copy of `str' and replace its first character with `#\a'
(define (make-copy str)
(string-set! (string-copy str) 0 #\a))
instead of:
(define (make-copy str)
(let ((s (string-copy str)))
(begin
(string-set! s 0 #\a)
s)))
or perhaps I am doing something wrong?
PS: the documentation of `list-set!' doesn't say what's its return value.
Best regards,
Alexandru Cojocaru
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: return value of `string-set!'
2013-08-14 22:33 return value of `string-set!' Alexandru Cojocaru
@ 2013-08-15 5:29 ` William ML Leslie
2013-08-17 12:52 ` Ludovic Courtès
2013-08-15 20:22 ` Taylan Ulrich B.
1 sibling, 1 reply; 4+ messages in thread
From: William ML Leslie @ 2013-08-15 5:29 UTC (permalink / raw)
To: Alexandru Cojocaru, guile-devel
On 15 August 2013 08:33, Alexandru Cojocaru <xojoc@gmx.com> wrote:
> Hi,
> currently `string-set!' returns an unspecified value. What about making it
> return `str' instead?
Functions whose specific purpose is to mutate the provided value
usually don't return the value because that is a common source of bugs
- this a common pattern in the string code and generally, too.
> Here is an example of its use:
>
> ; make a copy of `str' and replace its first character with `#\a'
> (define (make-copy str)
> (string-set! (string-copy str) 0 #\a))
>
> instead of:
>
> (define (make-copy str)
> (let ((s (string-copy str)))
> (begin
> (string-set! s 0 #\a)
> s)))
>
> or perhaps I am doing something wrong?
>
> PS: the documentation of `list-set!' doesn't say what's its return value.
Thanks for pointing that out! Curiously, it returns the value that
was set. I wonder if this was intentional.
> Best regards,
> Alexandru Cojocaru
--
William Leslie
Notice:
Likely much of this email is, by the nature of copyright, covered
under copyright law. You absolutely may reproduce any part of it in
accordance with the copyright law of the nation you are reading this
in. Any attempt to deny you those rights would be illegal without
prior contractual agreement.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: return value of `string-set!'
2013-08-14 22:33 return value of `string-set!' Alexandru Cojocaru
2013-08-15 5:29 ` William ML Leslie
@ 2013-08-15 20:22 ` Taylan Ulrich B.
1 sibling, 0 replies; 4+ messages in thread
From: Taylan Ulrich B. @ 2013-08-15 20:22 UTC (permalink / raw)
To: Alexandru Cojocaru; +Cc: guile-devel
Alexandru Cojocaru <xojoc@gmx.com> writes:
> (define (make-copy str)
> (let ((s (string-copy str)))
> (begin
> (string-set! s 0 #\a)
> s)))
FYI the `begin' there is redundant.
(define (make-copy str)
(let ((s (string-copy str)))
(string-set! s 0 #\a)
s))
This is idiomatic Scheme code; it makes it obvious that we're 1) getting
a value, 2) doing something to it, 3) returning it. It is immediately
obvious code, in contrast to the one-liner that relies on a procedure
meant for mutating an object to also return that object (which is one
sensible choice for its return value, but not immediately obvious, or
the only choice, or inherently logical). Of course this is all
subjective, and there could have been, for example, a convention that
all object-mutating procedures return the object they mutate, but that
is simply not the tradition/idiom; it's best to get used to the
established Scheme style. :)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: return value of `string-set!'
2013-08-15 5:29 ` William ML Leslie
@ 2013-08-17 12:52 ` Ludovic Courtès
0 siblings, 0 replies; 4+ messages in thread
From: Ludovic Courtès @ 2013-08-17 12:52 UTC (permalink / raw)
To: guile-devel
William ML Leslie <william.leslie.ttg@gmail.com> skribis:
> On 15 August 2013 08:33, Alexandru Cojocaru <xojoc@gmx.com> wrote:
>> Hi,
>> currently `string-set!' returns an unspecified value. What about making it
>> return `str' instead?
>
> Functions whose specific purpose is to mutate the provided value
> usually don't return the value because that is a common source of bugs
> - this a common pattern in the string code and generally, too.
Plus these functions are standard (RnRS, SRFI-13, etc.)
Ludo’.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-08-17 12:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-14 22:33 return value of `string-set!' Alexandru Cojocaru
2013-08-15 5:29 ` William ML Leslie
2013-08-17 12:52 ` Ludovic Courtès
2013-08-15 20:22 ` Taylan Ulrich B.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).