Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Last active August 29, 2015 14:24
Show Gist options
  • Save Heimdell/93c0d5eae95ca2a6a17f to your computer and use it in GitHub Desktop.
Save Heimdell/93c0d5eae95ca2a6a17f to your computer and use it in GitHub Desktop.
Не думаю, что программирование на Scheme имеет какой-либо эффект на программиста ;)
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]
(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