Question

I've to make a function that transforms an House to an NHouse.

data House = House { hworking :: Working, hfinished :: Finished}

type Working = [Roof] , type Finished = [Roof]

data NHouse = NHouse {rot :: [NRoof]} 

data NRoof = NRoof {h :: Roof, st :: Status }

data Status = Working | Finished

I've thought of doing it making an auxiliary function that transforms each Roof in an NRoof and then aply that to every Roof in the House.

But I just can't figure it out. I'm doing something like this:

nWorking :: Roof -> NRoof
nWorking x = NRoof {x, Working }
Was it helpful?

Solution

Yes you are going in the right direction. You can make a function to transform Roof to NRoof given the status.

transform :: Status -> Roof -> NRoof
transform s r = NRoof r s

Then you can just map this function over the list of roofs you have in the house.

h2n :: House -> NHouse
h2n (House w f) = NHouse $
                map (transform Working) w ++
                map (transform Finished) f

In one line this can be written as

h2n (House w f) = NHouse $ map (flip NRoof Working) w ++ map (flip NRoof Finished) f
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top