Scheme: Changing the definition of complex numbers to accept vectors

孤街浪徒 提交于 2019-11-29 16:29:00

Are you aware that the Racket reader already supports complex literal numbers?

Examples from the Reference:

1+2i reads equal to (make-complex 1 2)

1/2+3/4i reads equal to (make-complex (/ 1 2) (/ 3 4))

1.0+3.0e7i reads equal to (exact->inexact (make-complex 1 30000000))

In short: You can not with a "simple overwrite" accomplish what you want. The syntax for numbers include a + used to read/write complex numbers, but the reader converts 2+3i into a number directly with no option of any overriding.

If you want to use infix notation in your program you'll need to replace the reader. This is possible in Racket but it is more complicated than a simple overwrite.

Maybe you can use an existing library for infix notation in Racket?

You may want to try out:

https://github.com/soegaard/this-and-that/blob/master/readtable/test2.rkt

which parses anything in {} as infix expressions.

Modifying the Scheme reader is certainly not provided for in the Scheme standards and would thus require you to change the 'reader' for your chosen Scheme implementation - a non-trivial task.

You'd be better off embedding your own language in Scheme proper using Scheme's macro and abstraction facilities.

Assume you have a coordinate basis for i, j, and k and you want to express vectors as, for example, 2i+7j-k. You could start with:

(define (make-coord i j k) (vector i j k))
(define (coord-i b) (vector-ref b 0))
…

Then if you want to make things a bit easier to express:

(define-syntax coord
  (syntax-rules ()
    ((coord i j k) (make-coord i j k))))

which you would use as:

(define a-coord (coord 2 7 -1))

Edit: Here is an example. I replaced make-coord with list:

> (define-syntax coord
    (syntax-rules ()
      ((coord i j k) (list i j k))))
> (coord 1 1 1)
(1 1 1)

Note that one of the advantages to using syntax/functions, instead of the reader, is that you can use expressions:

> (coord (/ 124 4) 0 0)
(31 0 0)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!