1
votes

The full error is stated as "The result of this expression is implicitly ignored. Consider using 'ignore' e.g. 'expr |> ignore', or ' e.g. 'let result = expr'"

Full disclosure -> I am totally new to F# and trying to get up to speed on how to translate where applicable my knowledge of OOP to Functional Programming. I saw 1 other post on SO for this specific error but it didn't directly relate to my scenario.

module WebAPI =
let DoesSpecifiedHeaderExist(content: string[], searchString: string) = 
    if (content.Length > 0) then 
        for s in content do 
            if (s.Trim() = searchString.Trim()) then true else false
    else content |> ignore

Code requirement isn't complicated as I simply want to iterate through a string array searching for a value provided in searchString. If I find a match I return true otherwise return false. I'm sure there are other ways to do this but I'd really like to understand the issue.

The error is happening within the loop on the if..then..else block but I don't understand why I'm getting the error or how to correct the code. How is the result of the expression implicitly ignored when I'm explicitly returning either true or false on the condition and how can I correct it?

1

1 Answers

1
votes

The issue is that a for loop returns unit. So the type of

if (s.Trim() = searchString.Trim()) then true else false

is bool while the type of

for s in content do 
    if (s.Trim() = searchString.Trim()) then true else false

is unit.

So within the for loop you generate a true/false value and then do nothing with it, the result(s) never make it out of the loop.

For what your actually trying to do in this code you might want to look a List.tryFind. Something like this maybe:

let DoesSpecifiedHeaderExist(content: string[], searchString: string) = 
    match (List.tryFind (fun v -> v = searchString) content) with
    | Some s -> true
    | None -> false

So tryFind returns Some if it finds the search string; otherwise it returns None, the match converts the result to a Boolean. Another possibility is

List.tryFind (fun v -> v = searchString) content |> Option.isSome

Using the Option.isSome to convert the Option to a boolean.