斐波那契数在Haskell中

编程入门 行业动态 更新时间:2024-10-25 22:32:24
本文介绍了斐波那契数在Haskell中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我需要将我的F#代码更改为Haskell代码,但我在Haskell中是如此新颖,我不能这样做我的代码只是从键盘读取数据,如果数据不是整数返回错误消息,则计算n斐波那契数然后写入列表之后,将列表写入txt文件这里是我的代码

打开系统 let rec fib n = 匹配n与 | 0-> 0 | 1-> 1 | 2-> 1 | n-> fib( N-1)+ FIB(N-2);; 让printFibonacci list = 为i = 0 to(List.length list)-1 do printf%d(list.Item(i));; let writeToFile list = let file = System.IO.File.Create(C:\out2.txt) let mutable s =让writer = new System.IO.StreamWriter(file)尝试(对于i = 0)(List.length list)-1 do s< --list.Item(i)。 ToString() writer.Write(s +) finally writer.Close() file.Dispose() printfnWrited To文件 让mutable control = true 让mutable num = 0 while control do try printfn输入数字: num < - Convert.ToInt32(stdin.ReadLine()) let listFibonacci = [for i in 0 .. num-1-> fib(i)] printFibonacci(listFibonacci) printfn\\\%A(listFibonacci) writeToFile(listFibonacci) control< -false with | :? System.FormatException - > printfn数字格式异常; Console.ReadKey true |>忽略

解决方案 ($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ main = do putStrLn输入一个数字: num < - readLn fibs = map fib [0..n] mapM'print fibs

然而,由于haskell是懒惰的,所以有一个巧妙的方法来定义所有斐波那契数列表。既然你想要这个列表的前缀,那么使用这个定义更自然(也更有效):

fibs = 0 :1:zipWith(+)fibs(tail fibs) main = do putStrLn输入一个数字: num < - readLn mapM'print(take n fibs )

编辑:要写入文件而不是stdout,请替换 print (\ num - > appendFilefilename(show num))或(appendFilefilename。show) 。

Hi everbody I need to change my F# code to Haskell code but I am so new in Haskell and I can not this My code simply read data from keyboard if data not an integer return an error message then calculate the n fibonacci number then writes to a list after that writes the list into a txt file Here is my code

open System let rec fib n = match n with |0->0 |1->1 |2->1 |n->fib(n-1)+fib(n-2);; let printFibonacci list = for i=0 to (List.length list)-1 do printf "%d " (list.Item(i));; let writeToFile list = let file = System.IO.File.Create("C:\out2.txt") let mutable s ="" let writer = new System.IO.StreamWriter(file) try for i=0 to (List.length list)-1 do s <- list.Item(i).ToString() writer.Write(s+" ") finally writer.Close() file.Dispose() printfn "Writed To File" let mutable control = true let mutable num = 0 while control do try printfn "Enter a Number:" num <- Convert.ToInt32(stdin.ReadLine()) let listFibonacci = [for i in 0 .. num-1->fib(i)] printFibonacci(listFibonacci) printfn "\n%A"(listFibonacci) writeToFile(listFibonacci) control<-false with | :? System.FormatException->printfn "Number Format Exception"; Console.ReadKey true|>ignore

解决方案

fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) main = do putStrLn "Enter a number:" num <- readLn fibs = map fib [0..n] mapM' print fibs

However since haskell is lazy there is a clever way to define the list of all fibonacci numbers. And since you want a prefix of that list, it's more natural to use this definition (also more efficient):

fibs = 0 : 1 : zipWith (+) fibs (tail fibs) main = do putStrLn "Enter a number:" num <- readLn mapM' print (take n fibs)

Edit: To write to a file instead of stdout replace print with (\num -> appendFile "filename" (show num)) or (appendFile "filename" . show).

更多推荐

斐波那契数在Haskell中

本文发布于:2023-11-29 01:02:12,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Haskell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!