-
Notifications
You must be signed in to change notification settings - Fork 0
Brackets check #68
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
base: master
Are you sure you want to change the base?
Brackets check #68
Conversation
yurii-litvinov
left a comment
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.
В целом всё так. Зачтена
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В принципе, это всё можно было бы заменить Map-ом и вызывать соответствующие его методы
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Алгоритмически правильно, но технически лучше через match переписать. Вообще, rule of thumb таково, что List.head, List.tail и подобные функции редко встречаются где-то кроме параметров функций высших порядков, поэтому если их много, имеет смысл подумать, не будет ли код в полтора раза короче, если делать match по образцу (тут участвуют два списка, но никто не мешает матчить по паре).
No description provided.