Type annotation
I am trying to solve the Project Euler #8. Here is my solution:
open System
let product_of_digits num =
Seq.fold (fun acc elem -> acc * elem) 1 [for x in num -> int32(x) - 48]
let all_possible_strings number =
[ for x in [0 .. number.Length - 2] -> number.Substring(x, 2) ]
let problem_8 number =
Seq.max(
Seq.map (fun x -> (product_of_digits x)) (all_possible_strings number)
)
[<EntryPoint>]
let main(args : string[]) =
printfn "result = %d" (problem_8 "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047开发者_如何转开发48216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450")
0
All works fine in Fsi.exe, but when I am trying to compile the code I have got error:
D:\datahub\Dropbox\development\myprojects\project-euler\Problem_8\problem_8.fs(7,22): error FS0072: Lookup on object of
indeterminate type based on information prior to this program point. A type annotation may be needed prior to this progr
am point to constrain the type of the object. This may allow the lookup to be resolved.
D:\datahub\Dropbox\development\myprojects\project-euler\Problem_8\problem_8.fs(7,44): error FS0072: Lookup on object of
indeterminate type based on information prior to this program point. A type annotation may be needed prior to this progr
am point to constrain the type of the object. This may allow the lookup to be resolved.
How can I fix it?
For further insight, the reason why the F# compiler is having difficulty inferring the type of number
in all_possible_strings
is because you are using .NET object oriented instance properties and methods on number
, which don't give enough information about the type. However, you can swap out the property call number.Length
for the F# function application String.length number
which cascades so that now all functions and parameters will be fully inferred.
As the error says, you need to give the compiler more information about the type of your function arguments. If I count the lines right, it looks like the variable "number" in function "problem_8" is of indeterminate type. To fix it, reformulate the function definition like so (assuming you want your "number" argument to be a string):
let problem_8 (number : string) =
精彩评论