macrosracketinfix-operator

Racket custom 'infix' operator


I am still learning this. So my question title and this code both may be incorrect.

I am trying to define a new operator for appending. The inspiration for this is some OCaml code I am porting to Racket.

Compiles and executes but the test doesn't complete.

Test

(: fn1 ( Integer Integer -> Integer))
(define (fn1 x y)
  (displayln x)
  (+ x y)
 )

;; Test linspcm
( check-equal? (linspcm  '(0) (@) 1 2 fn1) '(1 2 3 4) "Test unsuccessfull")

Code

(define-syntax-rule (@) (append))

(define-syntax-rule  (linspcm z (@) x n f)
  (match n
    [0 z]
    [1 (f x)]
    [_ (let* ([m (/ n 2)])
         (displayln n)
         ((linspcm z (@) x m f) (@)
         (linspcm z (@) (+ x m) (- n m) f))
      )
    ]
  )
)

Is this feasible ?

Update : DrRacket runs out of memory.Earlier I used emacs.Macro expander does not end.


Solution

  • I realised after posting my question that the syntax-rule I needed is minimal. I also learnt based on the comments that the infix between two dots (. (@) .) becomes a prefix . The code that compiles and executes is

    (define-syntax-rule (@) append)
    
    (: linspcm : ((Listof Integer) Integer Integer
                  (Integer ->  Integer) -> (Listof Integer)))
    (define  (linspcm z  x n f)
    
      (match n
        [0 z]
        [1 (list (f x))]
        [_ (let* ([m (quotient n 2)])
             (displayln n)
             ((linspcm z    x m f) . (@) .
             (linspcm z  (+ x m) (- n m) f))
          )
        ]
      )
    )
    

    Note : This is my code experiment.