#lang plai ;;;; CS 214 Grad Lab 1: Church Numerals ;;; Pairs (define make-pair (lambda (a b) (lambda (sel) (sel a b)))) (define left (lambda (p) (p (lambda (a b) a)))) (define right (lambda (p) (p (lambda (a b) b)))) ;;; Church Numerals (define zero (lambda (f) (lambda (x) x))) (define zero? (lambda (cn) ((cn (lambda (ignored) false)) true))) (define one (lambda (f) (lambda (x) (f x)))) (define two (lambda (f) (lambda (x) (f (f x))))) (define succ (lambda (cn) (lambda (f) (lambda (x) (f ((cn f) x)))))) (define sum1 (lambda (cn1) (lambda (cn2) (lambda (f) (lambda (x) ((cn1 f) ((cn2 f) x))))))) (define sum2 (lambda (cn1) (lambda (cn2) ((cn1 succ) cn2)))) (define prod (lambda (cn1) (lambda (cn2) ((cn1 (sum1 cn2)) zero)))) ;; subtraction (define f1 (lambda (p) (make-pair (right p) (succ (right p))))) (define pred (lambda (cn) (left ((cn f1) (make-pair zero zero))))) ;;; Part1: CN->num (define cn->num (lambda (cn) ((cn add1) 0))) ;;; Part1: num->CN (define num->cn (lambda (num) (cond [(eq? num 0) zero] [else (succ (num->cn (sub1 num)))]))) ;;; Part1: subtraction (define subtraction (lambda (cn1) (lambda (cn2) ((cn2 pred) cn1)))) ;;; Testing (test (zero? zero) true) (test (zero? one) false) ;;; NOTE TO STUDENTS: Once you write "cn->num", uncomment the below tests by ;;; selecting the below text, then choosing Racket > Uncomment. ;;; Tests of cn->num (test (cn->num (succ zero)) 1) (test (cn->num (succ zero)) 1) (test (cn->num (succ two)) 3) ;;; Tests of sum1 (test (cn->num ((sum1 zero) zero)) 0) (test (cn->num ((sum1 one) one)) 2) (test (cn->num ((sum1 one) two)) 3) (test (cn->num ((sum1 two) two)) 4) ;;; Tests of sum2 (test (cn->num ((sum2 zero) zero)) 0) (test (cn->num ((sum2 one) one)) 2) (test (cn->num ((sum2 one) two)) 3) (test (cn->num ((sum2 two) two)) 4) ;;; Tests of prod (test (cn->num ((prod zero) zero)) 0) (test (cn->num ((prod zero) two)) 0) (test (cn->num ((prod two) zero)) 0) (test (cn->num ((prod two) one)) 2) (test (cn->num ((prod two) two)) 4) ;;; Tests of pred (test (cn->num (pred two)) 1) (test (cn->num (pred one)) 0) ;;; Part1.2: Tests of num->cn (test (num->cn 0) zero) (test (cn->num (num->cn 0)) 0) (test (cn->num (num->cn 1)) 1) (test (cn->num (num->cn 3)) 3) ;;; Part1.3: Tests of subtraction (test (cn->num ((subtraction zero) zero)) 0) (test (cn->num ((subtraction two) one)) 1) (test (cn->num ((subtraction (succ two)) two)) 1) (test (cn->num ((subtraction (succ two)) one)) 2)
Write, Run & Share Racket code online using OneCompiler's Racket online compiler for free. It's one of the robust, feature-rich online compilers for Racket language, running on the latest version 6.8. Getting started with the OneCompiler's Racket compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Racket
and start coding.
OneCompiler's Racket online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Racket program which takes name as input and print your name with hello.
#lang racket/base
(define name (read))
(printf "Hello ~a.\n" name)
Racket is a general-purpose programming language based on the Scheme dialect of Lisp. It is also used for scripting, computer science education, and research related applications.
Item | Decsription |
---|---|
; | To comment a single line |
;; | to mark important comments |
#; | to comment the following s-expression |
Data-type | Decsription |
---|---|
Numbers | represents integers, float and complex numbers |
Boolean | #t and #f are the two boolean literals |
Strings | To represent sequence of characters and double quotes("") are used to represent strings |
let and define are used to declare variables
(let ([id value-expression] ...) body ...+)
(let proc-id ([id init-expression] ...) body ...+)
define id expression
> (let ([x 10]) x)
10
If, If-else are used when you want to perform a certain set of operations based on conditional expressions.
(if cond-expr then-expr)
(if cond-expr then-expr else-expr)
For loop is used to iterate a set of statements based on a condition.
(for (for-clause ...) body-or-break ... body)
where
for-clause = [id seq-expr] | [(id ...) seq-expr] | #:when guard-expr | #:unless guard-expr | break-clause
break-clause = #:break guard-expr | #:final guard-expr
body-or-break = body | break-clause
seq-expr : sequence?
A lambda expression is used to create a function.
(lambda (argument-id ...)
body ...+)