How to generate a list of all possible strings from shortest to longest

前端 未结 4 1196
滥情空心
滥情空心 2021-02-09 05:03

I need to generate an infinite list of strings using numbers and letters. The first string should simply be \"a\", then \"b\" through to \"z\", then \"0\" through to \"9\", the

4条回答
  •  轮回少年
    2021-02-09 05:54

    I came up with the following solution:

    -- Gives us the concatenated cartesian product of two lists of strings
    strCartProd :: [String] -> [String] -> [String]
    strCartProd xs ys = [x ++ y | x <- xs, y <- ys]
    
    -- Create initial list of strings (map chars to 1-char strings)
    s :: [String]
    s = map (:[]) $ ['a'..'z'] ++ ['0'..'9']
    
    -- Iterate and concatenate the resulting lists
    result :: [String]
    result = id =<< iterate (strCartProd s) [""]
    

提交回复
热议问题