Short and sweet, my issue is that I am parsing a list of numbers ([Integer]), and I want to transform that list into nested tuples, so for example the list [1,2,3,4] would be (1,(2,(3,4))) this seems to me to be doable with a fold operation. I think the problem I am having is that the type is not determined before hand, making the tuple nest potentially infinite.
EDIT BASED ON COMMENTS
Thanks for the good replies, the root problem is for an assignment, so that is why details of the greater issue is sparse, but in an effort to not leave you wondering I can expand a bit, although I am not looking for answers to the expansion. My problem boils down to an issue with a right recursive and right associative part of a context free grammar, where I have something like
A -> n A
so I could get an expression like so 'n n n n A' which parses to 'n(n(n(n A)))' (A does have other terminals). I can parse it to '[n,n,n,n] A', which is why I wanted the conversion (I know there is a better way, I am just struggling to find it).
data T = Empty | Nonempty (Integer, T)to represent the arbitrary nesting of tuples, but in doing that we are only giving another name to the standard list type, so there's nothing to gain. Why do you think you need tuples for your data?data NTup t = Here t | There (NTup (Integer,t)). Then you can useNTup ()for a nested tuple of any size. By the time you recurse down toHere,twill be a nested tuple of the appropriate size (tryshowing it to see). This is called a non-regular type, and I can't really see a point to doing it in this instance, but it's a good technique to know.