[racket-dev] how to define a custom "null" in typed racket

From: Danny Yoo (dyoo at hashcollision.org)
Date: Thu Apr 11 18:32:06 EDT 2013

Perhaps something like this?


;;;;;;;;;;;;;
#lang typed/racket/base

(provide mynull mycar mycdr)

(struct: MyNull ())
(define mynull (MyNull))

(define-type MyListof (All (A) (Rec X (U MyNull (Pair A X)))))

(: mycar : (All (A) ((MyListof A) -> A)))
(define (mycar lst)
  (if (MyNull? lst)
      (error 'mycar "given list is empty")
      (car lst)))

(: mycdr : (All (A) ((MyListof A) -> (MyListof A))))
(define (mycdr lst)
  (if (MyNull? lst)
      (error 'mycdr "given list is empty")
      (cdr lst)))
;;;;;;;;;;;;;

where mynull is the singleton instance of the opaque structure type MyNull.

Posted on the dev mailing list.