首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从F#中的文本文件读取输入--用Newline的问题阅读输入

从F#中的文本文件读取输入--用Newline的问题阅读输入
EN

Stack Overflow用户
提问于 2022-11-02 17:58:09
回答 1查看 39关注 0票数 2

这是F#中Euler #8项目的代码。

我的问题是,我阅读的是一个由\n新行字符分隔的大(1000位)数字。然而,即使当我加入换行符上的“字符串”时,我仍然会收到错误。我已经用了两种不同的方法,但我仍然收到同样的错误信息。

代码语言:javascript
复制
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))

输出:

代码语言:javascript
复制
<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

数字是:

代码语言:javascript
复制
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-02 18:31:14

调用String.Join("\n", ...)时,将向结果字符串中插入换行符。然后,稍后,在其中一条新行上调用int64 << string。但不能将换行符转换为整数。如果您使用""作为String.Join中的分隔符,而不是"\n",则不会出现此问题。(或者只调用String.Concat而不是String.Join)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74293747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档