[racket] fuzzy set representation in Racket
Rian Shams wrote at 08/23/2013 03:49 PM:
>
> Any guidance on how to create fuzzy-set representation this way or if
> you can suggest more concise and elegant methods I would be greatly
> appreciative.
The representation you described looked OK to me -- you'd just have to
provide operations that enforced unique keys for set membership when
adding a member -- but here are some alternatives:
1. Old-school Lisp association list (aka alist), without a struct around
it. I would probably use this if the sets were small.
(list (cons 'a 0.1)
(cons 'b 0.2)
(cons 'c 0.3))
;;==> ((a . 0.1) (b . 0.2) (c . 0.3))
2. Racket immutable hash. I would probably use this if the sets were large.
(make-immutable-hasheq (list (cons 'a 0.1)
(cons 'b 0.2)
(cons 'c 0.3)))
;;==> #hasheq((a . 0.1) (c . 0.3) (b . 0.2))
3. Racket mutable hash. I would probably use this if the sets were
large and I wanted them mutable for some reason.
(make-hasheq (list (cons 'a 0.1)
(cons 'b 0.2)
(cons 'c 0.3)))
;;==> #hasheq((a . 0.1) (c . 0.3) (b . 0.2))
4. Typed Racket (TR), probably with struct containing alist or hash. If
you're looking for Racket to enforce your mathematical definition in
face of programming mistake, you might prefer TR. TR can also do some
numerical optimizations, which might be a big win if you're doing heavy
number-crunching, but you have to be careful not to define TR types that
are expensive for TR to test, especially if you're passing those types
between TR and non-TR Racket code.
(I'll leave the exact example to a TR person, since they know
optimization considerations that I don't.)
Neil V.