0

I have a TernaryTree type.

data TernaryTree a = Node a (TernaryTree a) (TernaryTree a) (TernaryTree a) | Nil deriving (Eq, Show)

Given a list, I want to be able to apply the Node constructor to it, e.g.

Node v [t1, t2, t3]  -- error

The above is, of course, invalid.

There are good ways through pattern matching to do the opposite, e.g

let Node v t1 t2 t3 = parent in [t1,t2,t3]

but I could not find a simple solution to my problem other than writing a function, e.g.

makeNode v lst@[t1,t2,t3] = Node v t1 t2 t3

Even if I had the function, it is not flexible, e.g. it cannot do this,

Node v t1 [t2, t3]  -- error

I now require another function.

I could add further constructors to the type for this but it seems to me that there must be a simpler way.

The type of the operation that would do what I want is

[a] -> a a a

which, I think, should be undefined.

This seems so simple a requirement that I am sure there is an easy built-in way to do this but I just cannot find it.

Is there a built-in way to use a constructor on a list?

wsaleem
  • 594
  • 8
  • 25
  • Why would you want to do that ? What if the list provides not enough arguments to the constructor ? List length is not a property that is checkable at compile time. You can use tuples for something similar and safe, and it's called currying. – baxbaxwalanuksiwe Apr 22 '17 at 15:21
  • How would a tuple be used for this? – wsaleem Apr 22 '17 at 15:35

0 Answers0