Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Created October 4, 2015 19:34
Show Gist options
  • Save Heimdell/42d17bcd8d2b56fc6a4f to your computer and use it in GitHub Desktop.
Save Heimdell/42d17bcd8d2b56fc6a4f to your computer and use it in GitHub Desktop.
#lang racket
(define (partition good? list return)
(match list
[(list)
(return `() `())]
[(list head tail ...)
(partition good? tail (λ (accepted rejected)
(if (good? head)
(return (cons head accepted) rejected)
(return accepted (cons head rejected)))))]))
(partition (curry < 4) `(1 9 2 8 3 7 4 6 5) (λ (good bad)
(printf "good: ~a; bad ~a~%" good bad)))
(define (split-at n list return)
(if ((zero? n) . or . (null? list))
(return `() list)
; else
(match list [(list head tail ...)
(split-at (n . - . 1) tail
(λ (before after)
(return (cons head before) after)))])))
(split-at 5 `(1 2 3 4 5 6 7 8 9) (λ (before after)
(printf "before: ~a; after ~a~%" before after)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment