I wonder pattern matching a set in Racket is possible? My certain case is algebraic identities, for example, 2*a can be a+a. However, currently, I match on list then I have to write (or (list '* 2 a) (list '* a 2)). If matching on set is possible, then I can have (set '* 2 a), which is easier.
2
votes
1 Answers
3
votes
You can use list-no-order. Use it only for small sets though.
In programs that needs to work on mathematical expressions, a standard technique is to "normalize" expressions before using any matching. Her "normalize" means to "sort" the arguments according to some order.
In racket-cas normalization will put numbers before symbols. And sort the symbols in alphabetical order.
#lang racket
(require racket-cas)
(normalize '(+ b 3 a))
The result will be:
(+ 3 a b)
If you are interested in writing a CAS on you own, I recommend buying or borrowing the two books by Cohen.
https://www.amazon.com/Joel-S-Cohen/e/B001K8MI06/ref=dp_byline_cont_pop_ebooks_1