【发布时间】:2021-02-10 08:31:09
【问题描述】:
我自己解决这个问题,四个小时后,我接近完成第 3.1.1 部分,但无法解释为什么我不能让它返回布尔值,特别是因为类似的函数很乐意做字符串连接。
(*
3.1 A time of day can be represented as a
triple (hours, minutes, f)
where f is either AM or PM – or as a record.
Declare a function to test whether one time of day comes
before another.
For example, (11,59,"AM") comes before (1,15,"PM").
Make solutions with triples as well as with records.
Declare the functions in infix notation.
Hansen, Michael R.; Rischel, Hans.
Functional Programming Using F# (p. 66).
Cambridge University Press. Kindle Edition.
*)
type TimeOfDayTriple = int * int * string
let (<.) ((hour1, min1, pod1):TimeOfDayTriple) ((hour2, min2, pod2):TimeOfDayTriple) =
true
//val ( <. ) : int * int * string -> int * int * string -> bool
let (<.) ((hour1, min1, pod1):TimeOfDayTriple) ((hour2, min2, pod2):TimeOfDayTriple) =
(pod1, pod2)
//val ( <. ) : int * int * string -> int * int * string -> string * string
let (<.) ((hour1, min1, pod1):TimeOfDayTriple) ((hour2, min2, pod2):TimeOfDayTriple) =
pod1 + pod2
val ( <. ) : int * int * string -> int * int * string -> string
let (<.) ((hour1, min1, pod1):TimeOfDayTriple) ((hour2, min2, pod2):TimeOfDayTriple) =
pod1 < pod2;;
//Ch03.fsx(32,5): error FS0001: This expression was expected to have type
// 'int * int * string'
//but here has type
// 'string'
【问题讨论】:
-
你有没有在上面的某个地方重新定义运算符
(<)?