chansey97
2021-9-15 10:21:47

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)))


ben.knoble
2021-9-15 11:39:09

I might write (let-values ([(u v) (if (zero? flag) (values 123 'abc) (values 456 'def))]) (f u v))


ben.knoble
2021-9-15 11:39:49

Or swap let for an internal define depending on how I’m feeling


chansey97
2021-9-15 11:59:11

let-values is nice. Thanks.


badkins
2021-9-15 14:00:08

(if (zero? flag) (f 123 'abc) (f 456 'def))


sschwarzer
2021-9-15 14:26:46

@sschwarzer has joined the channel


chansey97
2021-9-15 14:31:51

@badkins Yeah, but I mean the “play-with” is not just one procedure. It might be a bunch of expressions.


chansey97
2021-9-15 14:32:13

let-values is great, very clean!


badkins
2021-9-15 14:38:39

You could put the bunch of expressions in f


capfredf
2021-9-15 14:53:44

define-values could save you some parentheses (and indentations)


greg
2021-9-15 18:23:17

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
2021-9-15 18:37:48

@oleks.litus has joined the channel