這是來自 F# 中的 Project Euler #8 的代碼。
我的問題是我正在讀取一個由 \n 換行符分隔的大(1000 位)數字。但是,即使我在換行符上加入“字串”,我仍然會遇到錯誤。我已經以兩種不同的方式接近它,但我仍然收到相同的錯誤訊息。
open System;
open System.Text;
let path = "/Users/Arbin/Desktop/VS Code/F#/Project Euler/Largest Product in Series/largest_product_text_file"
//let monster_number = System.IO.File.ReadAllText path
let monster_number_array = System.IO.File.ReadAllLines path
let monster_number = String.Join("\n", System.IO.File.ReadAllLines path);
printfn "%s" monster_number
let adjacent n seq =
seq |> Seq.mapi (fun index value -> seq |> Seq.skip index |> (Seq.truncate n))
//Mapping numbers from string to integer64.
let seq_of_seq = (adjacent 13 monster_number) |> Seq.map (Seq.map (int64 << string))
//Iterating through a sequence of sequence (nested sequence)
seq_of_seq |> Seq.iter (fun x -> x |> Seq.iter (fun y -> printfn "Ar: %A" y))
輸出:
<Sequence Integers>
System.FormatException: Input string was not in a correct format.
at Microsoft.FSharp.Core.LanguagePrimitives.ParseInt64(String s) in D:\a\_work\1\s\src\FSharp.Core\prim-types.fs:line 2414
at Microsoft.FSharp.Collections.Internal.IEnumerator.map@99.DoMoveNext(b& curr) in D:\a\_work\1\s\src\FSharp.Core\seq.fs:line 102
at Microsoft.FSharp.Collections.Internal.IEnumerator.MapEnumerator`1.System.Collections.IEnumerator.MoveNext() in D:\a\_work\1\s\src\FSharp.Core\seq.fs:line 84
at Microsoft.FSharp.Collections.SeqModule.Iterate[T](FSharpFunc`2 action, IEnumerable`1 source) in D:\a\_work\1\s\src\FSharp.Core\seq.fs:line 596
at FSI_0001.it@20.Invoke(IEnumerable`1 x) in /Users/Arbin/Desktop/VS Code/F#/Project Euler/Largest Product in Series/largest_product_in_series.fsx:line 20
at Microsoft.FSharp.Collections.SeqModule.Iterate[T](FSharpFunc`2 action, IEnumerable`1 source) in D:\a\_work\1\s\src\FSharp.Core\seq.fs:line 597
at <StartupCode$FSI_0001>.$FSI_0001.main@() in /Users/Arbin/Desktop/VS Code/F#/Project Euler/Largest Product in Series/largest_product_in_series.fsx:line 20
Stopped due to error
號碼是:
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
uj5u.com熱心網友回復:
當您呼叫 時String.Join("\n", ...),您將在結果字串中插入換行符。然后,稍后,您將呼叫int64 << string其中一個換行符。但是您不能將換行符轉換為整數。如果您在, 而不是"""中用作分隔符,則不會遇到此問題。(或者只是呼叫而不是。)String.Join"\nString.ConcatString.Join
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525816.html
標籤:文件解析F#
