为什么我的计算机不能让我打招呼呢? [英] Why won't my computer let me greet my customers?
问题描述
我正在编写一个应用程序来替换我们的技术支持部门.当客户需要技术支持时,我们将简单地寄给他一张包含此应用程序的软盘,他可以将其放入计算机的孔中并进行安装.他们只要输入他们的问题,程序就会输出解决方案.
我尝试用F#编写它,但是F#不喜欢它.我写了这个简单的递归函数,向客户显示了一条问候消息,但是F#告诉我不,这段代码很糟糕".我非常有信心自己的代码很好,并且无法弄清楚为什么F#认为它是如此糟糕.
这是我的代码:
open System
let rec GreetCustomer message =
let DisplayMessage message =
Console.WriteLine(message + " " : string) |> ignore
GreetCustomer
DisplayMessage(x)
Console.WriteLine("Please do the needful by telling us your name?");
let CustomerName = Console.ReadLine()
GreetCustomer("Hello,")(CustomerName)("!")("How")("to")("help")("you")("today?")
F#告诉我
类型不匹配.期望为'a但给定为'b->'a统一'a'和'b->'a'
时,结果类型将是无限的
当然,结果类型是无限的.我希望能够无限次地链接方法调用.我不明白为什么F#不喜欢无限类型.我可以用Javascript编写相同的程序,而不会出现任何问题:
GreetCustomer = function(message) {
DisplayMessage = function(message) {
document.write(message + " ");
return GreetCustomer;
};
return DisplayMessage(message);
};
CustomerName = prompt("Please do the needful by telling us your name?");
GreetCustomer("Hello,")(CustomerName)("!")("How")("to")("help")("you")("today?");
它恰好具有我想要的输出:
你好,彼得!今天如何为您提供帮助?
如果它可以在Javascript中运行,那么肯定有一种方法可以在F#中实现.
我该如何修复我的F#程序,以便它不会抱怨无限类型?
我在评论中链接的问题对此进行了详尽的阐述.在F#中有多种方法可以执行此操作,但是没有一种方法可以为您提供所需的语法.以下答案与它的结果差不多.
https://stackoverflow.com/questions/2679547/function-which-returns -itself/2684005#2684005
https://stackoverflow.com/questions/2679547/function-which-returns-itself/2679578#2679578
编辑
这是我首选的解决方法(结合我链接的问题的答案):
type Inf<'T> = delegate of 'T -> Inf<'T>
let rec makeInfinite f =
fun x ->
f x |> ignore
Inf(makeInfinite f)
let (+>) (inf:Inf<_>) arg = inf.Invoke(arg)
let GreetCustomer = makeInfinite (printf "%s")
GreetCustomer "Hello " +> "there, " +> "how " +> "are " +> "you?" |> ignore
I am writing an application to replace our technical support department. When a customer needs technical support, we will simply mail him a floppy disk containing this application, which he can simply put into his computer hole and install it. They just type in their problem and the program will output the solution.
I tried writing it in F#, but F# doesn't like it. I wrote this simple recursive function that shows a greeting message to the customer, but F# tells me "no, this code is bad". I'm quite confident that my code is good, and can't figure out why F# thinks it's so bad.
This is my code:
open System
let rec GreetCustomer message =
let DisplayMessage message =
Console.WriteLine(message + " " : string) |> ignore
GreetCustomer
DisplayMessage(x)
Console.WriteLine("Please do the needful by telling us your name?");
let CustomerName = Console.ReadLine()
GreetCustomer("Hello,")(CustomerName)("!")("How")("to")("help")("you")("today?")
and F# tells me
Type mismatch. Expecting a 'a but given a 'b -> 'a The resulting type would be infinite when unifying ''a' and ''b -> 'a'
Of course the resulting type is infinite. I want to be able to chain the method calls up to an infinite number of times. I don't understand why F# doesn't like infinite types; I can write the same program in Javascript without any issues:
GreetCustomer = function(message) {
DisplayMessage = function(message) {
document.write(message + " ");
return GreetCustomer;
};
return DisplayMessage(message);
};
CustomerName = prompt("Please do the needful by telling us your name?");
GreetCustomer("Hello,")(CustomerName)("!")("How")("to")("help")("you")("today?");
and it has exactly the output I want:
Hello, Peter ! How to help you today?
If it works in Javascript, surely there must be a way to do it in F#.
How can I fix my F# program so that it won't complain about infinite types?
The question I linked in the comments elaborates on this extensively. There are ways to do this in F#, but none will give you the syntax you want. The following answers are about as good as it gets.
https://stackoverflow.com/questions/2679547/function-which-returns-itself/2684005#2684005
https://stackoverflow.com/questions/2679547/function-which-returns-itself/2679578#2679578
In short, this is a limitation of type inference. As the compiler complains, the type of a function which returns itself cannot be solved; it's infinite.
EDIT
This is my preferred workaround (combining answers from the question I linked):
type Inf<'T> = delegate of 'T -> Inf<'T>
let rec makeInfinite f =
fun x ->
f x |> ignore
Inf(makeInfinite f)
let (+>) (inf:Inf<_>) arg = inf.Invoke(arg)
let GreetCustomer = makeInfinite (printf "%s")
GreetCustomer "Hello " +> "there, " +> "how " +> "are " +> "you?" |> ignore
这篇关于为什么我的计算机不能让我打招呼呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!