Greetings,
I'm struggling to find a way to define an "Alist" once, and then simply add-in "Fields" to it incrementally.. without having to keep using new identifiers/variables to hold the result of each "addin" expression.
I understand that pure Functional Programming doesn't use destructive state changes, but I'm wondering if there is a way to make this happen using Monads, for example (which I have a cursory understanding of..)
Instead of doing this:
type Root = String
type Oct = Integer
type Mode = Integer
data Field = Root Root | Oct Oct | Mode Mode deriving (Show)
type Alist = [Field]
addin :: Alist -> Field -> Alist
addin p f = f:p
p0 :: Alist
p0 = []
p1 = addin p0 (Root "c")
p2 = addin p1 (Oct 4)
p3 = addin p2 (Mode 3)
p4 = addin p3 (Oct 3)
-- ... p42, etc
For the Alist version, when adding-in a Field with the same constructor-name as one that has already been added-in, it adds-in a brand new entry (with a repeated key-name (in the form of another constructor) and its associated value), as a normal association list does..
I'm also looking to define a Property-List version, where a Field-name/constructor can update a previous value (if the Field has already been added), or add-in a new entry if the Field hasn't been added yet.
Many Thanks,
Tom Jordan