Additional Dig Deeper Approach for F# Hamming Exercise

Hey,

Would like to know if this would be a valuable addition as an Approach in mentioned F# exercise?
If so, I can add a PR.

let distance (strand1: string) (strand2: string) : int option =
    if strand1.Length <> strand2.Length
    then None
    else
        [ for idx in 0 .. strand1.Length - 1 do
            yield strand1[idx] <> strand2[idx] ]
        |> List.sumBy Convert.ToInt32
        |> Some

I like the idea of having an approach with a list comprehension. Personally, I’m not a fan of converting booleans to integers, that to me feels a more suitable to low-level languages like C or dynamic languages like Python.

let distance (strand1: string) (strand2: string) : int option =
    if strand1.Length <> strand2.Length
    then None
    else
        [ for idx in 0 .. strand1.Length - 1 do
            yield if strand1[idx] = strand2[idx] then 0 else 1 ]
        |> List.sum
        |> Some

I’d be happy to have a separate section in the approach that showcases Convert.ToInt32, but I am hesitant to make the core code of the approach.

Avoiding converting booleans to integers might be a good idea, I agree. I’ll let you know if I have a PR!

1 Like

I’d expect using zip to be more idiomatic here. @aage did you choose the range instead to showcase it as an alternative to the already featured zip?

@MatthijsBlom Yes, zip (and recursion) are already featured.

PR: Add list comprehension approach to Hamming exercise by aage · Pull Request #1192 · exercism/fsharp · GitHub