Home > Net >  How to use arbitrary for a infinite list?
How to use arbitrary for a infinite list?

Time:03-29

i have the following help function:

    -- Generator for variable names
instance Arbitrary VarName where
  arbitrary = VarName <$> elements ["A", "B", "_0", "_"]

for the following task: "Define a function freshVars :: [Varname], which gives you an infinite list of variablenames in the following pattern:

[VarName "A",…,VarName "Z",VarName "A0",…,VarName "Z0",VarName "A1",…,VarName "Z1",…]"

How can i do this? i think i dont understand arbitrary properly.

CodePudding user response:

We can make a list with ["", "0", "1", …, "9", "10", "11", …, "99", …] with:

suffixes :: [String]
suffixes = "" : map show [0 ..]

Now that we have that, we can make a list comprehension that will construct strings with a prefix Character that will enumerate over ['A' .. 'Z'] and uses one of the items of the suffix. We can do that with list comprehension:

freshVars :: [String]
freshVars = [VarName (… : …) | … <- …, … <- … ]

where I leave filling in the parts as an exercise.

  • Related