tags:

views:

185

answers:

2

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
+1  A: 

That is basically the most common implementation of the sequence itself:

fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

If you want the number from keyboard:

main :: IO ()
main = catch doFib handle
   where doFib = do
            num <- readLn
            putStrLn $ "Fib of " ++ show num ++ " = " ++ (show . fib $ num)
         handle _ -> putStrLn "Malformed input!"

Done!

LukeN
Are you really recommending an algorithm with a complexity of O(2^n)? It is going to be horribly slow for anything other than small values of n.
Yacoby
Not recommending! Just saying it's the most basic, and (most importantly here, as he is new to haskell!) easiest implementation.
LukeN
@Yacoby: that's an exact translation of OP's algorithm implemented in F#, so it's a translation, not a recommendation.
yairchu
+8  A: 
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).

sepp2k
How to write the list to file
boraer
Thank you so much body
boraer