Den tor. 17. jun. 2021 kl. 15.37 skrev Yossu Bonkers <[email protected]>:
> I'm quite new to racket, so please pardon me if this is a dumb question. > > I'm working my way through the first tutorial in Beautiful Racket > <https://beautifulracket.com/stacker/intro.html>, and want to extend it. > He only provides + and * as operators. I added support for others. > > The problem is that I need to specify the operators I support in two > places, first where they are used in the handle function (see the > highlighted line)... > > (define > <http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))> > (handle [arg #f]) > (cond > <http://docs.racket-lang.org/reference/if.html#(form._((lib._racket/private/letstx-scheme..rkt)._cond))> > [(number? > <http://docs.racket-lang.org/reference/number-types.html#(def._((quote._~23~25kernel)._number~3f))> > arg) (push-stack! arg)] > * [(or > <http://docs.racket-lang.org/reference/if.html#(form._((lib._racket/private/letstx-scheme..rkt)._or))> > (equal? > <http://docs.racket-lang.org/reference/Equality.html#(def._((quote._~23~25kernel)._equal~3f))> > * > <http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._*))> > arg) (equal? > <http://docs.racket-lang.org/reference/Equality.html#(def._((quote._~23~25kernel)._equal~3f))> > + > <http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._+))> > arg))* > (define > <http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))> > op-result (arg (pop-stack!) (pop-stack!))) > (push-stack! op-result)])) > > ..and then again when they are provided... > > (provide > <http://docs.racket-lang.org/reference/require.html#(form._((lib._racket/private/base..rkt)._provide))> > + > <http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._+))> > * > <http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._*))> > ) > > I was wondering if it would be possible to specify the supported operators > in a list... > > (define my-funs '(f1 f2)) > > ...which could be used in handle, and then provide the functions in the > list. > > > You can't simple do... > > (provide my-funs) > > ...as this is a list of symbols. Equally you can't iterate the list and > call provide on each member, as provide has to be a top-level function. > > So, is there any way to provide the functions in the list? > I'll give you the big picture. As you have observed, `provide` is a compile time construct and can't be used at runtime (where the list my-funs lives). Since `provide` lives at compile time, the only solution is to use macros. Define a macro, say, define-binary-function that can used as: (define-binary-function *) (define-binary-function +) Given that, one can always define a define-binary-functions, that can be used as: (define-binary-functions + *) and expands into multiple instances of define-binary-function. Now, what must define-binary-function do? It must: 1) expands to a provide of the function name 2) add the function name to a list that holds names (symbols) of all binary functions. You can accomplish 2) by having a global: (define all-binary-functions '()) and let define-binary-function expand to both a provide and to (set! all-binary-functions (cons 'id all-binary-functions)) where id is from the use (define-binary-function id). In the definition of handle, you will then need to make use of all-binary-functions. If you are familiar with macros, I hope the overview will give you a good start. If not, the overview skipped a lot of details, so you will need to look at an introduction to macros first. If you are interested, I'll be happy to send you a work-in-progress macro tutorial I am working. I am always looking for feedback to make it better. /Jens Axel -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CABefVgycHfj0B_M8ks8V_MWG_idaOcCVHu5ZVTL_v3DjpT77xg%40mail.gmail.com.

