Note: I have seen this with some code but not others so I’m confused at to when to expect it happening.
I have some basic code in an FSX script that I am experimenting with:
let hasBreachedTarget target total = if total >= target then true else false
let report hasBreached total =
if total |> hasBreached then Ok total else Error total
let loadUp target collection =
let hasBreachedTarget = hasBreachedTarget target
let report = report hasBreachedTarget
let rec load acc collection =
match collection with
| [] -> report acc
| [single] -> report (acc + single)
| head::tail ->
let newTotal = acc + head
if newTotal |> hasBreachedTarget then
report newTotal
else
load newTotal tail
load 0 collection
let result = [ 1; 5; 4; 3; 4; 5 ] |> loadUp 15
…which, when executed in the FSI via Ctrl+A and Alt+Enter, reports what I would expect:
val hasBreachedTarget: target: 'a -> total: 'a -> bool when 'a: comparison
val report: hasBreached: ('a -> bool) -> total: 'a -> Result<'a,'a>
val loadUp: target: int -> collection: int list -> Result<int,int>
val result: Result<int,int> = Ok 17
However, if I change the last bit of code to simply (without the ‘let result =’):
[ 1; 5; 4; 3; 4; 5 ] |> loadUp 15
… I get this result:
val hasBreachedTarget: target: 'a -> total: 'a -> bool when 'a: comparison
val report: hasBreached: ('a -> bool) -> total: 'a -> Result<'a,'a>
val loadUp: target: int -> collection: int list -> Result<int,int>
…with no ‘it’ value.
I thought the ‘it’ value was supposed to be the result of the last thing executed.
If I try something even simpler, like:
[ 1; 5; 4; 3; 4; 5 ] |> List.sum
…I get an ‘it’ value, but I don’t get ‘it’ with my code above.
Am I doing something wrong, or misinterpreting something?
Is it something to do with the recursive nature of the loadUp
function?
Note: Microsoft (R) F# Interactive version 12.5.0.0 for F# 7.0