access to fields in nested records

Tim Docker timd at macquarie.com.au
Thu Nov 6 03:06:27 EST 2003


(Apologies for previous incomplete post: Here's the whole thing)


One thing that bugs me about the named record syntax, is that with a datatype:

data T = T {
    t_f1 :: X,
    t_f2 :: X
};

the function t_f1 has the type of an "accessor", ie

	t_f1 :: T -> X

but there doesn't any tidy way to get at the "mutator" function

	t_f1' :: X -> T -> T

without actually having to write the following 

	t_f1' x t = t{t_f1=x}

or is there?

Actually mutators of the more general form 

	t_f1' :: (X->X) -> T -> T
	t_f1' f t = t{t_f1=f (t_f1 t)}

would seem to be quite useful, (even though they are tedious to write)
as you can compose them for nested structures, ie if

data X = X {
    x_f1 :: Int,
    x_f2 :: Int 
};

Then...

		(t_f1'.x_f2') (const 5)

would be the functional equivalent of the imperative t.f1.f2 = 5


Tim



More information about the Haskell mailing list