[icfp] some example code

This commit is contained in:
ben 2016-03-20 00:06:07 -04:00
parent 51260412e3
commit 7e0064d7ba
4 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,8 @@
#lang typed/racket/base
;(define-new-subtype Database (database (Listof Table)))
;;; Issue 1: need (Listof T *)
;;(define-new-subtype Table (table (Pairof Symbol (Listof T))))
;(define-new-subtype Table (table (Pairof Symbol (Listof Any))))
;
;(define-type Me (Database (Table 'word (List Natural String))))

View File

@ -0,0 +1,9 @@
#lang typed/racket/base
(require trivial/no-colon)
(define (cite (p : String) (d : String))
(printf "~a v. ~a, U.S.\n" p d))
(define plaintiff*
'("Mistretta" "Rasul" "Chisholm"))
(define defendant*
'("U.S." "Bush" "Georgia"))
(void (map cite plaintiff* defendant*))

View File

@ -0,0 +1,18 @@
#lang typed/racket/base
;; Apply seems to work fine with arity checking.
;; That's good because I'd rather the type system tracked arity
;; than me reflecting it from a `define`.
;; -----------------------------------------------------------------------------
;; Works great
;(define (f x y)
; (+ x y))
;
;(apply f '(1 3 4))
(define (cons3 x y z)
(cons x (cons y z)))
(map cons3 '() '() '())

View File

@ -0,0 +1,25 @@
#lang typed/racket/base
;; Example using the db library in Typed Racket
;; - require/typed gives specific type
;; - rename-in allows multiple specific types in a file (just need to deal with renamings)
;; -----------------------------------------------------------------------------
(require/typed db
(#:opaque Connection connection?)
(postgresql-connect (->* () (#:user String #:database String) Connection)))
(require/typed (prefix-in w: db)
(w:query-row (-> Connection String Any * (Vector Natural String))))
(require/typed (prefix-in s: db)
(s:query-row (-> Connection String Any * (Vector Natural Natural))))
;; -----------------------------------------------------------------------------
(define c (postgresql-connect #:user "ben" #:database "ipoe"))
(define r0 (w:query-row c "SELECT * FROM word WHERE word.word=$1" "blossoms"))
(define r1 (s:query-row c "SELECT * FROM word_syllables WHERE word_syllables.word=$1" (vector-ref r0 0)))
(displayln r0)
(displayln r1)