开发者

Can't seem to find what's wrong with this piece of code... F#

I have checked everywhere and I can't seem to locate the problem. The compiler is giving me this error: "error FS0039: The value or constructor 'dotProduct' is not defined". But dotProduct is currently defined.

Visual Studio 2010 is also highlighting the second let (let rec dotProductAux list1 list2 acum =) saying that the expression is unfinished.

let dotProduct list1 list2 =
    let rec dotProductAux list1 list2 acum =
        match list1 ,list2 with
          | [],l | l,[] -> acum
          | head1 :: tail1, head2 :: tail2 -> let updated = (head1 * head2) + acum
                                     开发者_高级运维         (dotProductAux tail1 tail2 updated)

This code multiplies and adds two list like this:

dotProduct [1;4;7] [3;4;1];;   //(1*3) + (4*4) + (7*1)

I'm fairly new to F# and can't seem to get this code right. Any help?


The body of dotProduct contains the definition of dotProductAux, but no actual expression. You need to actually call dotProductAux (i.e. you're missing the call dotProductAux list1 list2 0 after the let rec).

Furthermore the case | [],[]| l,[] -> accum will cause an error because the second pattern binds the variable l, while the first does not. You can fix that by replacing l with _, since you don't actually need it. (Apparently you've already fixed this in an edit).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜