打印列表错误“('列表 - >'b列表)不兼容”

时间:2016-09-03 19:57:50

标签: f#

所以我收到以下错误消息:

The type '('a list -> 'b list)' is not compatible with any of the types
byte,int16,int32,int64,sbyte,uint16,uint32,uint64,nativeint,unativeint, 
arising from the use of a printf-style format string

这是设置它的代码:

let rec multC c = function
  | [] -> []
  | head::tail -> c * head::multC c tail


let p1 = [1; 2; 3];;
let resMultC = multC p1
printfn "resMultC: %d" resMultC

我无法为我的爱找出为什么它不会打印它,我认为这是错误的意思。任何提示?

1 个答案:

答案 0 :(得分:2)

如果您在FSI中检查multC签名c:int -> _arg1:int list -> int list。这意味着它需要两个参数(一个显式声明为c,另一个隐式来自function声明)。

那说你的代码的问题是你只提供一个参数

let resMultC = multC p1

而不是两个

let resMultC = multC 2 p1 // [2; 4; 6]

但即使是现在最后一次调用也不会编译,因为您尝试使用int格式化程序(%d)打印列表。使用%A代替F#特定类型:

printfn "resMultC: %A" resMultC // resMultC: [2; 4; 6]
相关问题