gpt4 book ai didi

f# - 如何在 F# 中编写 Fizzbuzz

转载 作者:行者123 更新时间:2023-12-03 11:03:11 25 4
gpt4 key购买 nike

我目前正在学习 F# 并尝试了(一个非常)简单的 FizzBu​​zz 示例。

这是我的初步尝试:

for x in 1..100 do 
if x % 3 = 0 && x % 5 = 0 then printfn "FizzBuzz"
elif x % 3 = 0 then printfn "Fizz"
elif x % 5 = 0 then printfn "Buzz"
else printfn "%d" x

什么解决方案可以使用 F# 来解决这个问题更优雅/简单/更好(解释原因)?

注意:FizzBu​​zz 问题是通过数字 1 到 100,每 3 的倍数打印 Fizz,每 5 的倍数打印 Buzz,3 和 5 的每个倍数打印 FizzBu​​zz。否则,将显示简单的数字。

谢谢 :)

最佳答案

我认为您已经有了“最佳”解决方案。

如果你想炫耀更多的功能/F#-isms,你可以做例如

[1..100] 
|> Seq.map (function
| x when x%5=0 && x%3=0 -> "FizzBuzz"
| x when x%3=0 -> "Fizz"
| x when x%5=0 -> "Buzz"
| x -> string x)
|> Seq.iter (printfn "%s")

并使用列表、序列、映射、迭代器、模式和部分应用程序。
[1..100]    // I am the list of numbers 1-100.  
// F# has immutable singly-linked lists.
// List literals use square brackets.

|> // I am the pipeline operator.
// "x |> f" is just another way to write "f x".
// It is a common idiom to "pipe" data through
// a bunch of transformative functions.

Seq.map // "Seq" means "sequence", in F# such sequences
// are just another name for IEnumerable<T>.
// "map" is a function in the "Seq" module that
// applies a function to every element of a
// sequence, returning a new sequence of results.

(function // The function keyword is one way to
// write a lambda, it means the same
// thing as "fun z -> match z with".
// "fun" starts a lambda.
// "match expr with" starts a pattern
// match, that then has |cases.

| x when x%5=0 && x%3=0
// I'm a pattern. The pattern is "x", which is
// just an identifier pattern that matches any
// value and binds the name (x) to that value.
// The "when" clause is a guard - the pattern
// will only match if the guard predicate is true.

-> "FizzBuzz"
// After each pattern is "-> expr" which is
// the thing evaluated if the pattern matches.
// If this pattern matches, we return that
// string literal "FizzBuzz".

| x when x%3=0 -> "Fizz"
// Patterns are evaluated in order, just like
// if...elif...elif...else, which is why we did
// the 'divisble-by-both' check first.

| x when x%5=0 -> "Buzz"
| x -> string x)
// "string" is a function that converts its argument
// to a string. F# is statically-typed, so all the
// patterns have to evaluate to the same type, so the
// return value of the map call can be e.g. an
// IEnumerable<string> (aka seq<string>).

|> // Another pipeline; pipe the prior sequence into...

Seq.iter // iter applies a function to every element of a
// sequence, but the function should return "unit"
// (like "void"), and iter itself returns unit.
// Whereas sequences are lazy, "iter" will "force"
// the sequence since it needs to apply the function
// to each element only for its effects.

(printfn "%s")
// F# has type-safe printing; printfn "%s" expr
// requires expr to have type string. Usual kind of
// %d for integers, etc. Here we have partially
// applied printfn, it's a function still expecting
// the string, so this is a one-argument function
// that is appropriate to hand to iter. Hurrah!

关于f# - 如何在 F# 中编写 Fizzbuzz,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2422697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com