Thursday 24 September 2020

(practical-python->racket 2.1 Datatypes and Data Structures)

 2.1 Datatypes and Data Structures

This is quite a short section that introduces Python's tuples and dictionaries. The exercises are all designed to be run in the Python console. I have included my solutions to the exercises in the datastructures.rkt file.

Primitive Datatypes

The three datatypes referred to as primitive (integers, floating point numbers and strings(text) and their Racket equivalents were covered in the introduction.

None Type

There is quite a difference Python's none type and Racket's. Python's is called None and is an object, Racket's is called null and is a constant that is defined to be an empty list. Python's None is used for optional or missing values and evaluates as false in conditional statements. As far as I can tell, null is used  much less frequently in Racket. It evaluates to true in conditional expressions.

Data Structures

Two data structures (or collections) are introduced in this section, tuples and dictionaries.

Tuples

A Python tuple is bascially an immutable list of values, exactly the same as a Racket list. Python tuples use the same syntax as Python lists for accessing individual elements. They tend to be used in lieu of structures by Python programmers though Python's named tuples are more akin to Racket's structures.

Racket doesn't seem to have anything like Python's tuple packing and unpacking built in. Unpacking is assigning the values of a tuple to multiple variables in a single statement. That could well be because having lots of individually named pieces of data isn't at all common in Racket.

Dictionaries

A Python dictionary can be described as an associative array or a hash table. Racket's hash tables provide its equivalent (and more). Racket also has dictionaries which appear to be at a higher-level that includes hash tables.

It seems that there are six types of hash table in Racket depending on how the hash keys are compared with each other and whether the table is immutable. The make-hash function creates mutable hash tables where the hashed keys are compared using the equal? function. I am using "make-hash" hash tables to compare with Python's dictionaries.

Racket allows more types of values as keys in a hash then Python. For example, in Racket you can use a list as a hash key but you can't in Python.

There is no simplified literal syntax to create a mutable hash tables in Racket that is equivalent to Python's JSON-like Dictionary syntax. (Though there is for Racket's immutable hash tables.)

So the equivalent of Python's 

    s = {

        'name': 'GOOG',

        'shares': 100,

        'price': 490.1

    }

is

    (define s (make-hash))

    (hash-set! s "name" "GOOG")

    (hash-set! s "shares" 100)

    (hash-set! s "price" 490.1)

Python uses a typical [] array notation to access individual dictionary modules such as:

  s['price'] 

The equivalent in Racket requires a call to the hash-ref function:

  (hash-ref s "price")

Python uses the same [] syntax to modify the value stored for a key, Racket uses the hash-set! function (as seen above).

Python has a del function which can be used to remove an entry from a dictionary, the equivalent in Racket is the hash-remove! function.

Exercises

The exercises are based on simple processing of data stored in csv format. Emulating most of the Python in Racket was straight forward, with the obvious exception of tuple unpacking.

There were a couple of things that are worthy of note. The first is that Python dictionaries are sequenced in the order that the keys were added. Racket's hashes are unordered.

Secondly, Racket's hash-keys function provides a simple list of the keys whereas Python's keys provides a dict_keys object which is dynamically updated if the dictionary it was created has keys subsequently added or removed.

Next up, 2.2 Containers