我试着谷歌搜索这个,但我找不到一系列的文字引导我到我想做的事情.
我正在尝试解决Project Euler Problem 54,我有这个相当荒谬的功能:
let evaluate hand =
if isRoyalFlush hand then 9
elif isStraightFlush hand then 8
elif isFour hand then 7
elif isFullHouse hand then 6
elif isFlush hand then 5
elif isStraight hand then 4
elif isThree hand then 3
elif isTwoPair hand then 2
elif isPair hand then 1
else 0
Run Code Online (Sandbox Code Playgroud)
所有isSomething关键字都是采用string array并返回布尔值的函数.使用模式匹配是否有更优雅的方法?
这不起作用:
match true with
| isRoyalFlush hand -> 9
| isStraightFlush hand -> 8
// .. …Run Code Online (Sandbox Code Playgroud) 长话短说我试图创建某种国际象棋库,可以用于任何规模的棋盘,可能是int.MaxValue由int.MaxValue.生成无限的排名列表很容易:
static IEnumerable<int> GetRankNumbers()
{
for(int i = 1; ; i++)
yield return i;
}
Run Code Online (Sandbox Code Playgroud)
但是为了生成文件信件,我目前正在这样做:
static IEnumerable<string> GetFileLetters()
{
const string theLetters = "abcdefghjklmnopqrstuvwxyz"; // there is no 'i'.
for(int i = 0; ; i++)
{
var factor = i / theLetters.Length;
var index = i % theLetters.Length;
var sb = new StringBuilder();
sb.Append(theLetters[index]);
for(int j = 0; j < factor; j++)
{
sb.Append(theLetters[index]);
}
yield return sb.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
产生以下内容:
var x = GetFileLetters().Take(100); …Run Code Online (Sandbox Code Playgroud)