Skip to content

Instantly share code, notes, and snippets.

@anttih
Created August 20, 2013 10:40
Show Gist options
  • Save anttih/6279881 to your computer and use it in GitHub Desktop.
Save anttih/6279881 to your computer and use it in GitHub Desktop.
My first Typed Racket program: the first pieces of a parser.
#lang typed/racket
(provide (all-defined-out))
(define-type Stream (Listof Char))
(struct: Success ([match : Any] [rest : Stream]) #:transparent)
(struct: Fail () #:transparent)
(define-type Result (U Success Fail))
(define-type Parser (Stream -> Result))
(: any-char (Stream -> Result))
(define (any-char s)
(if (null? s)
(Fail)
(Success (first s) (rest s))))
(: all-of (Parser * -> Parser))
(define (all-of . tests)
(lambda (s)
(: loop (Result (Listof Parser) -> Result))
(define (loop c tests)
(if (null? tests)
c
(match ((first tests) s)
[(Success c cs) (loop (Success c cs) (rest tests))]
[_ (Fail)])))
(loop (Fail) tests)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment