The following two styles, which one is cleaner on your side? 1. more imperative style (define u #f)
(define v #f)
(cond
((= flag 0)
(set! u 123)
(set! v 'abc))
(else
(set! u 456)
(set! v 'def)))
(play-with u v)
2. more functional style (let ((u (if (= flag 0) 123 456))
(v (if (= flag 0) 'abc 'def))
(play-with u v)))
I might write (let-values ([(u v) (if (zero? flag) (values 123 'abc) (values 456 'def))])
(f u v))
Or swap let for an internal define depending on how I’m feeling
let-values is nice. Thanks.
(if (zero? flag)
(f 123 'abc)
(f 456 'def))
@sschwarzer has joined the channel
@badkins Yeah, but I mean the “play-with” is not just one procedure. It might be a bunch of expressions.
let-values is great, very clean!
You could put the bunch of expressions in f
define-values
could save you some parentheses (and indentations)
If it’s just 2 or 3 values, and very “local” implementation code, often I’ll do let-values
or define-values
as folks described above.
Otherwise/also, it can be nice to define a little one-off struct
(and then extract the values using match
). This would look exactly like @badkins’s example, if f
happened to be the struct name (that is, f
would be its constructor function name). And you could do e.g. (match-define (f u v) some-f)
elsewhere to extract the pieces.
@oleks.litus has joined the channel