multiple-value version of in-value

34 views
Skip to first unread message

Jos Koot

unread,
May 4, 2020, 6:28:11 AM5/4/20
to us...@racket-lang.org

 

Recently I needed a multiple value version of in-value.

Does something like that exist already?

I could not find it, so I made one:

 

(define-syntax (in-values stx)

(syntax-case stx ()

  ((_ expr)

#'(make-do-sequence

    (λ ()

     (values

      (λ (p) expr)

      (λ (p) #f)

      #t

      (λ (p) p)

      #f

      #f))))))

 

A pity that it is a syntax. It is possible to code it as a procedure, but I do not see an elegant way to do that without confusing a list with a multiple value.

 

I had:

 

File drac-plus-sant-is-jordi.rkt

 

(define digits '(0 1 2 3 4 5 6 7 8 9))

 

  (for*/list

   ((A (in-list digits)) (digits (in-value (remove A digits)))

    (C (in-list digits)) (digits (in-value (remove C digits)))

    (D (in-list digits)) (digits (in-value (remove D digits)))

    (N (in-list digits)) (digits (in-value (remove N digits)))

    (R (in-list digits)) (digits (in-value (remove R digits)))

    (S (in-list digits)) (digits (in-value (remove S digits)))

    (T (in-list digits))

    (DRAC  (in-value (+ (* 1000 D) (* 100 R) (* 10 A) C)))

    (SANT  (in-value (+ (* 1000 S) (* 100 A) (* 10 N) T)))

    (JØRDI (in-value (+ DRAC SANT))) ...

 

With syntax in-values I could simplify it to:

 

File drac-plus-sant-is-jordi-with-combi.rkt

 

  (for*/list

   ((combination (in-combinations '(0 1 2 3 4 5 6 7 8 9) 7))

    (permutation (in-permutations combination))

    ((A C D N R S T) (in-values (apply values permutation)))

    (DRAC  (in-value (+ (* 1000 D) (* 100 R) (* 10 A) C)))

    (SANT  (in-value (+ (* 1000 S) (* 100 A) (* 10 N) T)))

    (JØRDI (in-value (+ DRAC SANT)))

    (J (in-value (find-decimal-digit JØRDI 4)))

    (Ø (in-value (find-decimal-digit JØRDI 3)))

    (I (in-value (find-decimal-digit JØRDI 0))) ...

 

See https://github.com/joskoot/drac-sant-jordi for the complete programs.

 

Best wishes, Jos

Philip McGrath

unread,
May 4, 2020, 11:21:33 AM5/4/20
to Jos Koot, us...@racket-lang.org
My package `adjutor` has a few variants of this, like `in-value*`: https://docs.racket-lang.org/adjutor/Stable.html#(part._.Sequence_.Constructors)

They can all be used as first-class procedures, but that does involve a little runtime overhead, so they use `define-sequence-syntax` to cooperate directly with `for`-like forms when possible.

-Philip


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/5eafee34.1c69fb81.33cf0.0ce5%40mx.google.com.

Jos Koot

unread,
May 4, 2020, 12:21:06 PM5/4/20
to Philip McGrath, us...@racket-lang.org

Very nice. I have installed the package. Thanks very much,

Jos

Jos Koot

unread,
May 4, 2020, 10:12:08 PM5/4/20
to Philip McGrath, us...@racket-lang.org

To Philip McGrath, Hi again

 

I modified the clause

((id ...) (in-value* expr ...))

to the following in-values clause:

((id ...) (in-values expr))

 

where the expr is supposed to return as many values as ids.

I simplified the code such as to avoid syntax-parse,

because I do not (yet) understand all its powerful features.

I prefer writing code I can understand in all details.

😉 I should study on syntax-parse in due future 😉

 

(define-sequence-syntax in-values

(λ (stx)

  (raise-syntax-error 'in-values

   "can only be used in for forms" stx))

(λ (stx)

  (syntax-case stx ()

   (((id ...) (_ expr))

  #'((id ...)

     (:do-in

      (((id ...) expr))

      #t () #t () #t #f ()))))))

 

For example:

 

(for/first (((a b c) (in-values (values 2 + 3)))) (b a c)) ; -> 5

 

Thanks again, Jos

 

From: Philip McGrath
Sent: 04 May 2020 17:21
To: Jos Koot
Cc: us...@racket-lang.org
Subject: Re: [racket-users] multiple-value version of in-value

 

My package `adjutor` has a few variants of this, like `in-value*`: https://docs.racket-lang.org/adjutor/Stable.html#(part._.Sequence_.Constructors)

Philip McGrath

unread,
May 4, 2020, 10:21:37 PM5/4/20
to Jos Koot, us...@racket-lang.org
Glad it was useful!

There is a variant `in-value*/expression` that works like your `in-values`: https://docs.racket-lang.org/adjutor/Stable.html#(form._((lib._adjutor%2Fmain..rkt)._in-value*%2Fexpression))

-Philip

Reply all
Reply to author
Forward
0 new messages