Improve Racket code#21
Open
elibarzilay wants to merge 6 commits intologicchains:masterfrom
elibarzilay:master
Open
Improve Racket code#21elibarzilay wants to merge 6 commits intologicchains:masterfrom elibarzilay:master
elibarzilay wants to merge 6 commits intologicchains:masterfrom
elibarzilay:master
Conversation
The thing that tends to trip people is that the `integer?` predicate is true for both exact integers (like 123) *and* inexact ones (like 123.0), while Typed Racket's `Integer` corresponds only with the exact ones. So usually the easy way out of this is to use `exact-integer?` instead. As a side note, "The `if n` is like a form of pattern matching" -- that's not really true. There's no pattern matching happenning. Instead, once you use a predicate for a specific type with something in an `if`, Typed Racket knows that in the then-branch, something has that type. In the `if n` case, that means that it knows that it cannot be `#f`, and if outside the `if` it was `(U #f Whatever)` then in the then branch it is left as `Whatever`. This is called "occurrence typing".
* Use a plain `for` loop over the input instead of an indirect counter * Use `(cons x y)` to add things on the left side instead of `(append l (list x))` (if you really need to preserve the order, then it's better to `reverse` the lists when you're done reading) * Using plain `'` quote is generally preferred when you don't need the extra features of quasi-quotes * `str->int` is used only in `read-places`, so put it there
…lobal vector. Makes it three times slower, but much more readable code.
(Functional in the proper sense, not in the we-don't-mutate sense.)
Makes it roughly as fast as the global-vector version.
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.
Roughly same speed, but much better looking code. A bunch of smaller commits with explanations.