# Re: [racket-users] Defining algebraic data types?

```A few answers:

1. Use struct subtyping to give a type that includes both kinds:
(struct dual ())
(struct quaternion dual (scalar vector))
(struct dual-quaternon dual (real dual))```
```
2. Use a library that supports algebraic data types, such as
https://pkgs.racket-lang.org/package/datatype

Sam

On Sun, Feb 4, 2018 at 9:16 AM, HiPhish <hiph...@openmailbox.org> wrote:
> Hell Racketeers,
>
> I am trying to write an implementation of the mathematical concept of dual
> quaternions in Racket. Dual quaternions are an algebraic type and there are
> several equally valid way to look at them.
>
> Let me give some background first. A dual number (a + bε) is similar to a
> complex number (a + bi), except that the square of ε is 0 instead of -1. A
> quaternion (a + bi + cj + dk) is like a complex number, except that it has
> three complex units which all square to -1. A dual quaternion is either a
> dual
> number where both components are quaternions, or a quaternion where all four
> components are dual numbers. There are also "subtypes" of dual quaternoins:
> a
> dual scalar is a dual quaternions where the vector parts of the two
> quaternions
> are zero, and a dual vector (not to be confused with the dual vectors from
> linear algebra) is a dual quaternions where the scalar part of both
> quaternions
> is zero. Unlike the subtypes in OOP these algebraic subtypes have *less*
> information than their parent (kind of like a square is a rectangle defined
> by
> only one length instead of two). (If you want to get really down to it, dual
> quaternions are elements of the Clifford algebra Cl^+(0, 3, 1).)
>
> So my question is: what would be the best way of defining such algebraic
> types?
> At first I wanted to create a struct:
>
>     (define vector (i j k))
>     (struct quaternion (scalar vector))
>     (struct dual-quaternon (real dual))
>
> But this limits me to a "dual number where both components are quaternions"
> view of the type. It also means that each dual scalar has be a sort of
> mishapped dual quaternion.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an