Skip to content

Instantly share code, notes, and snippets.

@dyoo
dyoo / unfriendly-numbers.rkt
Created April 18, 2012 23:32
Unfriendly Numbers interviewstreet.com question in Racket
#lang racket/base
;; Unfriendly numbers
;; https://www.interviewstreet.com/challenges/dashboard/#problem/4f7272a8b9d15
;; We'll provide a single function to solve this problem.
;;
;; Note that this module itself can be run as a main program, due to the
;; "main" submodule at the bottom of this file.
;;
@dyoo
dyoo / test.rkt
Created May 4, 2012 16:53
test program to raise error
#lang racket/base
(require racket/gui/base
racket/class)
(define (do-the-build)
(define f (new frame% [label "Building..."]))
(define t (new text% [auto-wrap #t]))
(define c (new editor-canvas% [parent f] [editor t]))
(send f show #t)
(void (thread (lambda ()
@dyoo
dyoo / gist:3078092
Created July 9, 2012 18:37
Epsilon
#lang racket/base
;; http://en.wikipedia.org/wiki/Machine_epsilon
;; approximates the machine epsilon
(require racket/flonum)
(define (compute-machine-epsilon)
(let loop ([n 1.0])
(define new-n (fl/ n 2.0))
(cond
@dyoo
dyoo / def.rkt
Created August 7, 2012 20:57
proc/data
#lang racket
;; Let's create a custom structure for procedures that remember certain things.
(struct proc/data (proc data)
#:property prop:procedure (lambda (pd . args)
(apply (proc/data-proc pd) args)))
;; We can manually create such instances...
(define f (proc/data
;; implementation:
#lang racket
;; http://stackoverflow.com/questions/12345647/rewrite-this-script-by-designing-an-interpreter-in-racket
;; A pexpr is one of the following:
;; * a primitive string,
;; * a seq, or
;; * a repeat
(struct seq (bodies) #:transparent)
(struct repeat (n body) #:transparent)
@dyoo
dyoo / gist:3759037
Created September 20, 2012 23:59
Records - like structs but less ugly and much less efficient
#lang racket
(require rackunit
(for-syntax racket/syntax))
;Records - like structs but less ugly and much less efficient
(define-syntax (record stx)
(syntax-case stx ()
[(_ name slots ...)
(with-syntax ([new-name (format-id #'name "new-~a" (syntax-e #'name))])
@dyoo
dyoo / decimal-to-binary.rkt
Created September 22, 2012 06:13
decimal to binary fraction
#lang racket/base
(require racket/stream)
;; decimal-to-binary-fraction: positive-real-less-than-1 -> (sequenceof (U 0 1))
;; Computes the binary expansion of the fraction part of a number.
(define (decimal-to-binary-fraction n)
(let loop ([n n]
[current-fraction 1/2])
(cond [(= n 0)
@dyoo
dyoo / for-seq.rkt
Created September 24, 2012 03:18
for/seq for sequence generating through for loops
#lang racket
(require racket/generator)
(define-syntax (for/seq stx)
(syntax-case stx ()
[(_ clauses . body)
#`(in-generator
(for/fold/derived #,stx
()
clauses
@dyoo
dyoo / gist:3805375
Created September 29, 2012 22:59
Pascal's triangle memoized
#lang racket
;; http://www.cforcoding.com/2012/01/interview-programming-problems-done.html
;; A little syntax for memoizing functions like Pascal's triangle.
(define-syntax (define/memo stx)
(syntax-case stx ()
[(_ (name args ...) body ...)
(syntax/loc stx
@dyoo
dyoo / gist:3809206
Created October 1, 2012 02:45
counting words with tildes
#lang racket
;; Counts words, treating tilde-words as escaped words.
;; Based on the SO question:
;;
;; http://stackoverflow.com/questions/12666553/efficient-word-count-method-using-regex-or-something
(require rackunit)