Leandro Facchinetti

Playing the Game with PLT Redex

At a high level, PLT Redex is a tool for manipulating and visualizing S-expressions (identifiers, numbers, strings, lists, and so forth), which PLT Redex calls terms. In programming languages, terms represent programs and program fragments, types, machine states, and so forth. In Peg Solitaire, terms represent pegs, boards, and so forth. We use the term form to construct terms from Racket values, for example:

terms.rkt

#lang racket
(require redex)

(test-equal (term 0)
            0)
(test-equal (term "a")
            "a")
(test-equal (term a)
            'a)

We represent pegs with and spaces with :

(test-equal (term )
            '●)
(test-equal (term )
            '○)

We can group pegs and spaces together in lists:

(test-equal (term (  ))
            '(  ))

We can assign terms to names in PLT Redex with the define-term form. We can then refer to these names in other terms:

(define-term a-peg )
(test-equal (term a-peg)
            '●)

We represent a Peg Solitaire board as a list of rows; a row as a list of positions; and a position as either a peg (), a space () or a padding (·). The following are examples of boards:

(define-term example-board-1
  ([· ·    · ·]
   [· ·    · ·]
   [      ]
   [      ]
   [      ]
   [· ·    · ·]
   [· ·    · ·]))

(define-term example-board-2
  ([· ·    · ·]
   [· ·    · ·]
   [      ]
   [      ]
   [      ]
   [· ·    · ·]
   [· ·    · ·]))

The following is the initial board:

(define-term initial-board
  ([· ·    · ·]
   [· ·    · ·]
   [      ]
   [      ]
   [      ]
   [· ·    · ·]
   [· ·    · ·]))

And the following is an example of a winning board:

(define-term example-winning-board
  ([· ·    · ·]
   [· ·    · ·]
   [      ]
   [      ]
   [      ]
   [· ·    · ·]
   [· ·    · ·]))

We will use these boards for testing in later sections, so we provide them here:

(provide example-board-1 example-board-2
         initial-board example-winning-board)

Next, we explore the most common operation on terms, pattern matching.