2
votes

I'm trying to implement the macros -> and ->> in Scheme and my final result was:

  ;;
  ;; See https://clojuredocs.org/clojure.core/-%3E
  ;; for more info.
  ;;
  (define-syntax ->
    (syntax-rules ()
      ((_ a) a)
      ((_ a (b c ...)) (b a c ...))
      ((_ a b) (b a))      
      ((_ a b c) (-> (-> a b) c))
      ((_ a b ... c) (-> (-> a b ...) c))))

  ;;
  ;; See https://clojuredocs.org/clojure.core/-%3E%3E
  ;; for more info.
  ;;
  (define-syntax ->>
    (syntax-rules ()
      ((_ a) a)
      ((_ a (b c ...)) (b c ... a))
      ((_ a b) (b a))      
      ((_ a b c) (->> (->> a b) c))
      ((_ a b ... c) (->> (->> a b ...) c))))

I have made some tests successfully, but I am not sure if it is 100% correct and if it is 100% optimized.

Is it correct? Is there any way to optimize this? Did I do too much or is it anyway?

1

1 Answers

3
votes

The answer depends on the question: which Scheme dialect are you using? Because in Racket we already have threading macros, so there's no need to implement them :) . In particular, ~> and ~>> are supported, among others. Please take a look at the documentation.