【发布时间】:2010-03-05 18:16:15
【问题描述】:
我是 OCaml 新手。我正在玩“hello world”类型的 sn-ps 并遇到了这种情况。 这是一个带有一些额外 cmets 的解释器会话:
# let average a b =
(a +. b) /. 2.;;
val average : float -> float -> float = <fun>
# average 1. 4.;;
- : float = 2.5
# string_of_float (average 1. 4.);;
- : string = "2.5"
(* this fails...*)
# let _ = Printf.printf (string_of_float (average 1. 4.));;
Error: This expression has type string but an expression was expected of type
('a, out_channel, unit) format =
('a, out_channel, unit, unit, unit, unit) format6
(* yet this works *)
# "hello!";;
- : string = "hello!"
# let _ = Printf.printf "hello!";;
hello!- : unit = ()
(* another failed attempt *)
# let s = string_of_float (average 1. 4.);;
val s : string = "2.5"
# s;;
- : string = "2.5"
# let _ = Printf.printf s;;
Error: This expression has type string but an expression was expected of type
('a, out_channel, unit) format =
('a, out_channel, unit, unit, unit, unit) format6
(* and this also works?? *)
# let _ = Printf.printf "2.5";;
2.5- : unit = ()
所以情况就是这样。 string_of_float (average 1. 4.) 返回一个字符串,
就像"hello!" 一样。当我将"hello!" 输入Printf.printf 时,它可以工作
正如预期的那样。当我将string_of_float (average 1. 4.) 给Printf.printf 它失败并告诉
我预期的不是字符串,而是其他奇怪的类型。但是为什么"hello!" 和"2.5" 会起作用呢?
发生了什么事?
【问题讨论】: