Skip to content

Instantly share code, notes, and snippets.

@urbanslug
Created July 3, 2019 09:13
Show Gist options
  • Save urbanslug/2671d661be900eba847439498adf85b7 to your computer and use it in GitHub Desktop.
Save urbanslug/2671d661be900eba847439498adf85b7 to your computer and use it in GitHub Desktop.
#lang racket
;; TODO: represent bases in binary
(struct node* (sequence offset))
(provide
(contract-out [struct node* ((sequence string?) (offset (or/c natural? null)))]))
(provide (contract-out
[node (-> string?
(or/c natural? null?)
(struct/c node* string? (or/c natural? null?)))]))
;; define node as a fn
(define/contract (node sequence [offset null])
(node* sequence offset))
(node* 123 "dd") ;; succeeds
@jboynyc
Copy link

jboynyc commented Jul 3, 2019

alternative using typed/racket

#lang typed/racket

(provide (all-defined-out))

(struct node*
  ([sequence : String]
   [offset : (Option Natural)]))

(define (node [seq : String]
              [offset : (Option Natural) #f])
  (node* seq offset))

(node "foo" 123) ;; succeeds
(node "foo" #f) ;; succeeds
(node "foo") ;; succeeds
;(node 123 "dd") ;; type mismatch

Or if you want to use null:

#lang typed/racket

(provide (all-defined-out))

(define-type Natural-or-Null (U Natural Null))

(struct node*
  ([sequence : String]
   [offset : Natural-or-Null]))

(define (node [seq : String]
              [offset : Natural-or-Null null])
  (node* seq offset))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment