我正在尝试在Haskell中创建一个无限列表,该列表使用字母a..z,然后重复以1(“ a1 ... z1”)为后缀,然后重复2(“ a2” ..“ z2” )等
目前,我最了解的是使用列表推导从a-z和1-9的两个列表中生成每个字符串组合的列表, 但是我不知道如何将其应用于我的问题。我也尝试过使用map函数,但也无法弄清楚如何将其应用于我的问题
public class User
{
public string Name
{
get;
set;
}
public ObjectId ProfilePictureId
{
get;
set;
}
}
在ghci中键入variables :: [Var]
variables = [x : y | y <- "" : variables, x <- ['a'..'z'] ++ ['0'..'9']]
应该可以:
[variables !! i | i <- [0,1,25,26,27,100,3039]]
,但目前我得到的结果是:["a","b","z","a1","b1","w3","x116"]
。任何帮助将不胜感激
答案 0 :(得分:5)
使用一个无限序列:
Prelude> let variables = [l:show x | x <- [1..], l <- ['a'..'z']]
Prelude> take 100 variables
["a1","b1","c1","d1","e1","f1","g1","h1","i1","j1","k1","l1","m1","n1","o1","p1","q1","r1","s1","t1","u1","v1","w1","x1","y1","z1","a2","b2","c2","d2","e2","f2","g2","h2","i2","j2","k2","l2","m2","n2","o2","p2","q2","r2","s2","t2","u2","v2","w2","x2","y2","z2","a3","b3","c3","d3","e3","f3","g3","h3","i3","j3","k3","l3","m3","n3","o3","p3","q3","r3","s3","t3","u3","v3","w3","x3","y3","z3","a4","b4","c4","d4","e4","f4","g4","h4","i4","j4","k4","l4","m4","n4","o4","p4","q4","r4","s4","t4","u4","v4"]
只需在该列表上使用(++)
并将['a'..'z']
放在单个字符列表的前面,请注意,由于'a' :: char
您需要事先将其转换为String
Prelude> let variables = [l:[] | l <- ['a'..'z']] ++ [l:show x | x <- [1..], l <- ['a'..'z']]
Prelude> take 100 variables
["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","a1","b1","c1","d1","e1","f1","g1","h1","i1","j1","k1","l1","m1","n1","o1","p1","q1","r1","s1","t1","u1","v1","w1","x1","y1","z1","a2","b2","c2","d2","e2","f2","g2","h2","i2","j2","k2","l2","m2","n2","o2","p2","q2","r2","s2","t2","u2","v2","w2","x2","y2","z2","a3","b3","c3","d3","e3","f3","g3","h3","i3","j3","k3","l3","m3","n3","o3","p3","q3","r3","s3","t3","u3","v3"]