[Haskell-beginners] How to nest arbitrary things

Daniel Trstenjak daniel.trstenjak at gmail.com
Mon Dec 21 21:17:21 UTC 2015


On Mon, Dec 21, 2015 at 09:06:55PM +0100, Imants Cekusins wrote:
> > a lens with the name 'cans' will be created.
> 
> Does Template Haskell modify code file or can these '_' names be used
> throughout the code without visible definition?

In this case Template Haskell doesn't modify the code, fields like
'_cans' are just normal fields and can be used with this name.

There's only the convention that 'makeLenses' creates for every field
with a '_' prefix a lens.

In the case of '_cans' a lens like this will be created:

   cans :: Lens' Parcel [Can]
   cans = lens getCans setCans
      where
         getCans parcel      = _cans parcel
	 setCans parcel cans = parcel { _cans = cans }


(This is just an idealized implementation and the real one
will be most likely a bit more sophisticated and optimized.)

> Could we say that lens is a bit like extension to record syntax: allow to
> traverse, get and set properties for structures that are  more complex than
> simple 1 level record?

It's often compared with some kind of jQuery for data structures.

> Why do lens require Template Haskell? Only to generate '_' functions?

To automatically generate the Haskell code like the lens 'cans' in the
above example.


Greetings,
Daniel


More information about the Beginners mailing list