Search code examples
racketcontract

How to make a contract for a heterogeneous list with arbitrary number of values?


I am trying to make a contract for data that looks like this:

'(a (b c) (d e) ...) ; a, b, c, d, e are all symbols

which is basically a list consisting of a symbol followed by an arbitrary number of lists of two symbols.

There is list/c but that only lets me make it with a fixed number of elements. There is also *list/c which takes arbitrary initial values, followed by final fixed values, which is kind of the opposite of what I need.

How do I make a correct contract for my data structure?


Solution

  • You can use cons/c to apply one contract to the head of the list and another to the tail. What you want to express is that the head is a symbol and the tail is a list of pairs of symbols, so that'd be:

    (cons/c symbol? (listof (list/c symbol? symbol?)))