Home > Mobile >  writing a function that shows left associativity equals right associativity
writing a function that shows left associativity equals right associativity

Time:09-17

i have been trying to write a function that shows that left association is the same as right association. basically meaning that (x, (y, z)) is the same as ((x, y), z), and vice versa, for this case. i have tried using let and in logic but that didnt work. so far i have

lAssociation :: (a, (b, c)) -> ((a, b), c)
lAssociation (a, b) = ((a, fst b), snd b)

but i get a parse error for some reason. I'm not sure what I am missing. I am new to haskell and want to understand so any help, suggestions, or advice is greatly appreciated

CodePudding user response:

The original signature was:

lAssociation :: ((a, b), c) -> (a, (b, c))
lAssociation (a, b) = ((a, fst b), snd b)

since you use fst :: (a, b) -> a and snd :: (a, b) -> b, this thus means that b is a 2-tuple, and this thus means that the second item of the input type is a 2-tuple:

lAssociation :: (a, (b, c)) -> ((a, b), c)
lAssociation (a, b) = ((a, fst b), snd b)

I would however advise to use pattern matching over fst and snd, and thus work with:

lAssociation :: (a, (b, c)) -> ((a, b), c)
lAssociation (a, (b, c)) = -- …

CodePudding user response:

Your type and your implementation are mismatched. What you have written is

lAssociation :: (a, (b, c)) -> ((a, b), c)
  • Related