斐波那契数在Haskell中 [英] Fibonacci Numbers in Haskell

查看:167
本文介绍了斐波那契数在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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆