Last active
August 29, 2015 14:24
-
-
Save Heimdell/93c0d5eae95ca2a6a17f to your computer and use it in GitHub Desktop.
Не думаю, что программирование на Scheme имеет какой-либо эффект на программиста ;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list_induction list, stop, step, ret | |
return stop.(ret) if list.empty? | |
x = list.shift | |
return list_induction list, stop, step, -> (args) { | |
step.(x, args, ret) | |
} | |
end | |
list_induction [1,2,3,4,5], | |
-> (back) { back.(0) }, | |
-> (x, sum, back) { | |
back.(x + sum) | |
}, | |
-> (result) { | |
puts result | |
} | |
def partition list, pred | |
list_induction list, | |
-> (back) { back.([[], []]) }, | |
-> (x, (good, bad), back) { | |
if x.send pred then | |
back.([[x] + good, bad]) | |
else | |
back.([good, [x] + bad]) | |
end | |
}, | |
-> ((good, bad)) { | |
yield good, bad | |
} | |
end | |
partition [1,2,3,4,5], :odd? do |good, bad| | |
puts good: good, bad: bad | |
end | |
def as_reduce zero, add, list | |
list_induction list, | |
-> (back) { back.(zero) }, | |
-> (x, sum, back) { | |
back.(x.send(add, sum)) | |
}, | |
-> (it) { it } | |
end | |
puts as_reduce 0, :+, [1,2,3,4,5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define ((list-induction stop step) list) | |
(define (list-induction list) | |
(unlist list stop | |
(λ (x xs) | |
(step x (list-induction xs))))) | |
(list-induction list)) | |
(define (list-induction-cps lst stop step return) | |
(define (list-induction-cps-aux lst return) | |
(unlist lst | |
(λ () (stop return)) | |
(λ (x xs) | |
(list-induction-cps-aux xs (λ args2 | |
(apply step (append (list x) args2 (list return)))))))) | |
(list-induction-cps-aux lst return)) | |
(define (my-partition-1 lst predicate return) | |
(list-induction-cps lst | |
(λ (return) (return `() `())) | |
(λ (x good bad return) | |
(if (predicate x) | |
(return (cons x good) bad) | |
(return good (cons x bad)))) | |
return)) | |
(ln `---) | |
(my-partition-1 `(1 2 1 4 1) (curry equal? 1) (λ (good bad) | |
(ln `(good = ,good bad = ,bad)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment