我一直在嘗試編寫一個使用u =(u1,u2,...,un)和v =(v1,v2,..., vn)並輸出u1 * v1 + u2 * v2 + ... + un * vn。我覺得我有邏輯大多正確的(至少它會在其他語言...),但我不斷收到:F#函數 - 預期類型與實際不同
stdin(11,57): error FS0001: This expression was expected to have type
'a list
but here has type
'c list * 'd list -> 'b list
的代碼如下:這個問題顯然是在調用產品在最後一行。然而,我的印象是,基本情況(x * y):: []只會產生一個'列表,而不是實際產生的東西。
let rec multvec xs ys = function
| [ ], [ ] -> failwith "Both lists cannot be empty"
| x::[ ], y::[ ] -> (x * y)::[ ]
| x::xs, y::ys -> let product = multvec xs ys
(x * y) + (List.reduce (+) product)
任何澄清這個錯誤將不勝感激!先謝謝你。
謝謝!您列出的所有方法都非常出色。我想我需要閱讀更多介紹F#的文檔,因爲它與我習慣的其他語言非常不同。 – wmarquez 2013-03-03 07:21:00