Skip to content

Instantly share code, notes, and snippets.

@samth
Created March 24, 2012 20:48
Show Gist options
  • Save samth/2187635 to your computer and use it in GitHub Desktop.
Save samth/2187635 to your computer and use it in GitHub Desktop.
Higher-order functions in C and Racket via the FFI
#lang racket
(require ffi/unsafe srfi/67)
(define-fun-syntax _unptr
(syntax-rules () [(_ t) (type: _pointer pre: (x => (ptr-ref x t)))]))
(define qsort
(get-ffi-obj 'qsort #f
(_fun (l : (_list io _int len))
(len : _int = (length l))
(size : _int = (ctype-sizeof _int))
(compare : (_fun (_unptr _int) (_unptr _int) -> _int))
-> _void -> l)))
(qsort
'(7 1 2 3 5 6 4 8 0 9)
number-compare)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment