-
-
Notifications
You must be signed in to change notification settings - Fork 109
Add list comprehension approach to Hamming exercise #1192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ErikSchierboom
merged 1 commit into
exercism:main
from
aage:hamming-approach-list-comprehension
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
exercises/practice/hamming/.approaches/list-comprehension/content.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # List comprehension | ||
|
|
||
| ```fsharp | ||
| module Hamming | ||
|
|
||
| let distance (strand1: string) (strand2: string) : int option = | ||
|
|
||
| if strand1.Length <> strand2.Length | ||
| then None | ||
| else | ||
| [ for idx in 0 .. strand1.Length - 1 do | ||
| if strand1[idx] <> strand2[idx] then yield 1 else yield 0 ] | ||
| |> List.sum | ||
| |> Some | ||
| ``` | ||
|
|
||
| ## Error path | ||
|
|
||
| We start by checking if the strings have unequal lengths, and return `None` if so: | ||
|
|
||
| ```fsharp | ||
| if strand1.Length <> strand2.Length | ||
| then None | ||
| ``` | ||
|
|
||
| ```exercism/note | ||
| Note that we're using `string` class' `Length` property, not a function like `Seq.length`. | ||
| Even though F# is a functional-first language, you'll use types (like the `string` class) defined in the .NET framework, which is an object-oriented framework. | ||
| Inevitably, you'll thus use objects that have methods and properties defined on them. | ||
| Don't worry about using methods and objects though, F# is a multi-paradigm language and embraces the interaction with object-oriented code (like the `string` class). | ||
| ``` | ||
|
|
||
| ## Happy path | ||
|
|
||
| In the happy path, we know that the strings have the same length so we can use the length (minus one) of the first string as the max of a range of _indices_ to use to access each `char` of both `string`s and compare them: | ||
|
|
||
| ```fsharp | ||
| for idx in 0 .. strand1.Length - 1 do | ||
| ``` | ||
|
|
||
| The entire `for` expression is surrounded by square brackets (`[]`) indicating that this is a [List comprehension][list-comprehension]. | ||
| This gives you the power of returning intermediate results based on comparing each pair of `char`s (returning a `1` when they differ or a `0` if they don't) and then continuing the next pair until you reach the end: | ||
|
|
||
| ```fsharp | ||
| if strand1[idx] <> strand2[idx] then yield 1 else yield 0 | ||
| ``` | ||
|
|
||
| The `yield` keyword indicates that this concerns an intermediate result, this can also be used in [C#][yield-return]. | ||
|
|
||
| The resulting list of `1`'s and `0`'s is then _piped_ into a [List.sum][list.sum] to get the hamming distance and finally the result is wrapped in a `Some`. | ||
|
|
||
| ```fsharp | ||
| |> List.sum | ||
| |> Some | ||
| ``` | ||
|
|
||
| [list-comprehension]: https://en.wikibooks.org/wiki/F_Sharp_Programming/Lists#Using_List_Comprehensions | ||
| [list.sum]: https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/lists#arithmetic-operations-on-lists | ||
| [yield-return]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield |
8 changes: 8 additions & 0 deletions
8
exercises/practice/hamming/.approaches/list-comprehension/snippet.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| let distance (strand1: string) (strand2: string) : int option = | ||
| if strand1.Length <> strand2.Length | ||
| then None | ||
| else | ||
| [ for idx in 0 .. strand1.Length - 1 do | ||
| if strand1[idx] <> strand2[idx] then yield 1 else yield 0 ] | ||
| |> List.sum | ||
| |> Some | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,8 @@ | |
| "lestephane", | ||
| "robkeim", | ||
| "valentin-p", | ||
| "wolf99" | ||
| "wolf99", | ||
| "aage" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to keep in mind: lines that are too long are clipped.
some measurements on my end
@ErikSchierboom don't you think it would be better for the snippets to be horizontally scrollable rather than clipped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. Please open an issue on the forum