Open
Conversation
yurii-litvinov
approved these changes
Aug 24, 2020
Comment on lines
+7
to
+20
| let isNotBracket (ch : Char) = | ||
| ch <> '[' && ch <> '{' && ch <> '(' && ch <> ']' && ch <> '}' && ch <> ')' | ||
|
|
||
| /// Checks if symbol is a opening bracket | ||
| let isOpenBracket (bracket : Char) = | ||
| bracket = '[' || bracket = '{' || bracket = '(' | ||
|
|
||
| /// Checks if symbols are opening and closing brackets of the same type | ||
| let isMatchingBrackets (first : Char) (second : Char) = | ||
| match first with | ||
| | '(' -> second = ')' | ||
| | '[' -> second = ']' | ||
| | '{' -> second = '}' | ||
| | _ -> true |
There was a problem hiding this comment.
В принципе, это всё можно было бы заменить Map-ом и вызывать соответствующие его методы
Comment on lines
+24
to
+34
| if str = [] | ||
| then memory = [] | ||
| else | ||
| let current = List.head str | ||
| if (isNotBracket current) then | ||
| checkLine (List.tail str) (memory) | ||
| elif (isOpenBracket current) then | ||
| checkLine (List.tail str) (current :: memory) | ||
| elif (memory <> [] && isMatchingBrackets (List.head memory) (current)) then | ||
| checkLine (List.tail str) (List.tail memory) | ||
| else false |
There was a problem hiding this comment.
Алгоритмически правильно, но технически лучше через match переписать. Вообще, rule of thumb таково, что List.head, List.tail и подобные функции редко встречаются где-то кроме параметров функций высших порядков, поэтому если их много, имеет смысл подумать, не будет ли код в полтора раза короче, если делать match по образцу (тут участвуют два списка, но никто не мешает матчить по паре).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.