Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created December 24, 2013 18:39
Show Gist options
  • Save christianromney/8116652 to your computer and use it in GitHub Desktop.
Save christianromney/8116652 to your computer and use it in GitHub Desktop.
Accompany mex.pdf
#lang racket
;; Non-tail
(define sum1
(λ (n)
(if (= n 0) n
(+ n (sum1 (- n 1))))))
(sum1 10)
;; Tail call
(define sum2
(λ (n)
(letrec [(suma (λ (n acc)
(if (= n 0) acc
(suma (- n 1) (+ acc n)))))]
(suma n 0))))
(sum2 10)
;; Theorem
(define sum3
(λ (n)
(/ (+ n (* n n)) 2)))
(sum3 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment