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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…