Python to Racket - racket/racket GitHub Wiki
DRAFT Migrating from Python to Racket
Contents
Idioms
- A Python "List" is an extensible vector of pointers.
Performance pitfalls
-
Python Lists aren’t Racket Lists
- use either Growable Vectors or RaList
-
Python
append
isn't Racketappend
.- Python's
append
adds a single element to the back of the List in amortized O(1) time. - Racket's append returns the concatenation of two or more Lists in O(n) time.
- This makes it more similar to
extend
or+
in Python.
- This makes it more similar to
- Use the
cons
operator to add single elements to a Racket List (adding to the front of a Racket List is a O(1) operation). - Building a List from the front may result in elements being in reverse order, but it is faster and more
idiomatic in Racket to build a List from the front and then correct the order by calling
reverse
. While this may seem counter-intuitive,reverse
is relatively fast and won't change the time complexity of any O(n) or worse operations.
- Python's
How to transliterate the loop/append example from python to Racket:
optionally also show the more native Racket idiom
References
- Choosing a data-structure
- Notes on data structures in Racket, with some performance considerations.