Skip to content

Instantly share code, notes, and snippets.

View kmicinski's full-sized avatar

Kristopher Micinski kmicinski

View GitHub Profile
[program ([toplevel-rule program] (void))
([toplevel-fact program] (void))
([toplevel-rule] (void))
([toplevel-fact] (void))]
;; Rules
[rule ([lb hclause+ arrow bclause+ rb]
(wrap-prov
$1-start-pos
$5-end-pos
;; Small example of pseudo-algebraic effects in Racket
#lang racket
(define handler-tag (make-continuation-prompt-tag 'handler))
;; Handler
(define ((handler x) k)
(displayln (format "giving back value ~a" x))
(call-with-continuation-prompt
(lambda () (k x))
int foo() { return 1; }
int bar() { return 2; }
int main(int argc, char **argv)
{
if (argc > 2) { foo(); }
; Function Attrs: nobuiltin noinline nounwind
define i64 @foo(i64, i64, i64, i64, i64, i64, i64, i64) local_unnamed_addr #4 {
%9 = load volatile i64, i64* getelementptr inbounds (%struct.State, %struct.State* @__mcsema_reg_state, i64 0, i32 6, i32 13, i32 0, i32 0), align 8
%10 = icmp eq i64 %9, 0
#lang racket
;; Can make these more slick w/ higher-order functions
(define (elements< l n)
(match l
['() '()]
[`(,first ,rest ...) #:when (< first n)
(cons first (elements< rest n))]
[else (elements< (rest l) n)]))
;; Raises an event to the debugger
(define (raise-event cur-ir evt)
(abort-current-continuation 'debug cur-ir evt))
#lang racket
(define l `(,(+ 1 2) 3 hello))
(define l2 `(,(+ 1 2) 3 'hello))
(equal? l l2) ;; #f, there's an *extra* tick in l2
(define (tree? t)
(match t
[`(empty-tree) #t]
[`(node ,(? number? n)) #t]
#lang racket
;; Reverse a list using tail recursion
;; This is very natural using tail recursion,
;; since most things get reversed anyway.
(define (rev-lst l)
;; you probably want to use a helper function
(define (helper l acc)
'todo)
'todo)
#lang racket
;; Merge sort (imperative version)
;;
;; MergeSort(arr[], l, r)
;; If r > l
;; 1. Find the middle point to divide the array into two halves:
;; middle m = (l+r)/2
;; 2. Call mergeSort for first half:
;; Call mergeSort(arr, l, m)
#lang racket
;; Write a function that takes a list l, of numbers,
;; and returns a hash which maps each number to its
;; square
;;
;; hint: use a recursive (or tail-recursive) function
;; to loop over the elements of l and build up a hash
;; from each element, e, to (* e e). Finally, return
;; the hash.