I have implemented a Fibonacci Sequence generator as follows
let getNext upperLimit current=
let (e1, e2) = current
let next = e1 + e2
if next > upperLimit then None
else Some (next, (e2,next))
let fib upperLimit = (0,1) |> Seq.unfold (getNext upperLimit) |> Seq.append [0;1]
and my test code is
[<Test>]
member Spec.``fib not exeeding 20 should be 0,1,1,2,3,5,8,13``()=
let expected = seq [0;1;1;2;3;5;8;13]
let result = fib 20
let expectedSameAsResult = (expected = result)
printfn "Expected: %A Result: %A result length: %d" expected result (Seq.length result)
Assert.That expectedSameAsResult
The test failed and the printed result is
Expected: [0; 1; 1; 2; 3; 5; 8; 13] Result: seq [0; 1; 1; 2; ...] result length: 8
When I used a for loop to print every element in result, I got exact same elements in the expected sequence.
So, what is the difference between the expected and result sequence?
Edit: my implementation can be found at https://github.com/weima/EulerProblems/tree/master/EulerProblems
Edit: To answer John Palmer's answer I just wrote a test in F# interactive window
let a = seq[1;2;3]
let b = seq[1;2;3]
let c = a = b;;
The result I got is val a : seq = [1; 2; 3] val b : seq = [1; 2; 3] val c : bool = true
So F# can do structural comparison to sequences too.
Edit to reflect to Gene Belitski's answer I have changed the test to
[<Test>]
member Spec.``fib not exeeding 20 should be 0,1,1,2,3,5,8,13``()=
let expected = seq [0;1;1;2;3;5;8;13]
let result = Problem2.fib 20
let comparedResult = Seq.compareWith (fun a b -> a - b) expected result
let expectedSameAsResult = (comparedResult = 0)
Assert.That expectedSameAsResult
And it worked now. Thanks! but I still don't understand why a simple seq[1;2;3]=seq[1;2;3] works, but my test case doesn't.
a=(b|> Seq.map id);;- John Palmerseq {1..3} = seq {1..3}in FSI - this expression equals tofalse- Gene Belitski