Fibonacci Numbers in Haskell
Posted
by boraer
on Stack Overflow
See other posts from Stack Overflow
or by boraer
Published on 2010-05-10T15:28:29Z
Indexed on
2010/05/10
15:44 UTC
Read the original article
Hit count: 197
haskell
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
© Stack Overflow or respective owner