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

  • Python append isn't Racket append.

    • 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.
    • 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.

How to transliterate the loop/append example from python to Racket:

optionally also show the more native Racket idiom

References