f# - Why can't I Seq.take more than 5 values? -
whilst trying understand seq.unfold, have been playing following f# produces sequence of triangle numbers...
let tri_seq = 1.0 |> seq.unfold (fun x -> (0.5 * x * (x + 1.0), x + 1.0)) |> seq.map (fun n -> int n)
this seems work fine, in can following...
tri_seq |> seq.nth 10
...and shows right number, whatever value pass it.
now, i'm trying print out first (say) ten values in sequence, rather above code gets nth. tried following...
tri_seq |> seq.take 10 |> seq.map (fun n -> printfn "%d" n)
...but prints first 5 values. whatever use value passed seq.take, ever maximum of 5 results, though when using seq.nth, can go far like.
anyone able explain me? why can't past fifth value?
the problem you're using seq.map
print values. sequences lazy , resulting sequence never evaluated - see 5 values because f# interactive prints first 5 elements of sequence.
you can use seq.iter
iterates on whole sequence:
tri_seq |> seq.take 10 |> seq.iter (fun n -> printfn "%d" n)
the key difference seq.map
returns new sequence values produced function specify while seq.iter
not return , iterates on input.
in case, can use partial function application , make code bit shorter:
tri_seq |> seq.take 10 |> seq.iter (printfn "%d")
Comments
Post a Comment