continuation-passing

Continuation Passing Style in ocaml

半腔热情 提交于 2021-02-18 18:00:33
问题 I am a bit confused on the concept. So I have the following function let rec sumlist lst = match lst with | [] -> 0 | (h::t) -> h + (sumlist t) With continuation, it can be written as let rec cont_sumlist lst c = match lst with | [] -> (c 0) | (h::t) -> cont_sumlist t (fun x -> c (h + x)) I am still confused on what the c means and what it does 回答1: A general answer is already given, but specifically, for cont_sumlist , in case of [] we "return" (i.e. feed) 0 into c that we're given ( 0 is

Continuation Passing Style in ocaml

偶尔善良 提交于 2021-02-18 17:59:33
问题 I am a bit confused on the concept. So I have the following function let rec sumlist lst = match lst with | [] -> 0 | (h::t) -> h + (sumlist t) With continuation, it can be written as let rec cont_sumlist lst c = match lst with | [] -> (c 0) | (h::t) -> cont_sumlist t (fun x -> c (h + x)) I am still confused on what the c means and what it does 回答1: A general answer is already given, but specifically, for cont_sumlist , in case of [] we "return" (i.e. feed) 0 into c that we're given ( 0 is

Get a subtree by breadth-first index using continuation-passing style

落爺英雄遲暮 提交于 2021-01-04 06:38:07
问题 This question is a follow-up on How do I get a subtree by index?. That question deals with depth-first indexing (for which I have provided a depth-first continuation-passing style solution). This question here is about breadth-first indexing, and specifically about solving the problem using continuation-passing style (CPS). Suppose I have a tree that represents '(+ (* 5 6) (sqrt 3)) : The index of nodes starts from 0 at the root, and is breadth-first. In the picture above, I have labelled all

Get a subtree by breadth-first index using continuation-passing style

 ̄綄美尐妖づ 提交于 2021-01-04 06:37:10
问题 This question is a follow-up on How do I get a subtree by index?. That question deals with depth-first indexing (for which I have provided a depth-first continuation-passing style solution). This question here is about breadth-first indexing, and specifically about solving the problem using continuation-passing style (CPS). Suppose I have a tree that represents '(+ (* 5 6) (sqrt 3)) : The index of nodes starts from 0 at the root, and is breadth-first. In the picture above, I have labelled all

The Little Schemer evens-only*&co

筅森魡賤 提交于 2020-01-20 19:14:10
问题 I'm having difficulty understanding what's going on with The Little Schemer's evens-only*&co example on page 145. Here's the code: (define evens-only*&co (lambda (l col) (cond ((null? l) (col '() 1 0)) ((atom? (car l)) (cond ((even? (car l)) (evens-only*&co (cdr l) (lambda (newl product sum) (col (cons (car l) newl) (opx (car l) product) sum)))) (else (evens-only*&co (cdr l) (lambda (newl product sum) (col newl product (op+ (car l) sum))))))) (else (evens-only*&co (car l) (lambda (newl

Can a convolution function written in tail recursive form?

旧巷老猫 提交于 2020-01-13 18:23:05
问题 I have a function that I want to write in tail recursive form. The function calculates the number of ways to get the sum of k by rolling an s sided die n times. I have seen the mathematical solution for this function on this answer. It is as follows: My reference recursive implementation in R is: sum_ways <- function(n_times, k_sum, s_side) { if (k_sum < n_times || k_sum > n_times * s_side) { return(0) } else if (n_times == 1) { return(1) } else { sigma_values <- sapply( 1:s_side, function(j)

heap usage for CPS vs non-CPS parsers in Haskell's parsec

試著忘記壹切 提交于 2020-01-02 13:56:25
问题 I'm trying to write the following parser using parsec: manyLength :: forall s u m a. Monad m => ParsecT s u m a -> ParsecT s u m Int manyLength p = go 0 where go :: Int -> ParsecT s u m Int go !i = (p *> go (i + 1)) <|> pure i This is like the many function, but instead of returning [a] , it returns the number of times Parser a succeeds. This works, but I can't seem to make it run in constant heap space. This makes sense, since the recursive call to go is not in the tail-call position. If

SICP, Continuation Passing Style and Clojure's trampoline

╄→гoц情女王★ 提交于 2020-01-01 19:19:09
问题 I am working with SICP and exercise 2.29-b gave me the opportunity to have fun with the Continuation Passing Style while traversing mobiles and branches. To make the story short, each mobile has left and right branch, which are composed by a length and either a numeric weight or another mobile. The question asks to find the total weight given a mobile. After the first mutually recursive solution, quite simple, I tried and successfully implemented a cps' one: (defn total-weight-cps [mobile]

Why continuation passing style

别说谁变了你拦得住时间么 提交于 2019-12-31 08:58:12
问题 In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons: pass more than one result to its continuation, because the procedure that implements the continuation can take any number of arguments. CPS also allows a procedure to take separate continuations ..., which may accept different numbers of arguments. Since the first reason can also be done using the values procedure and the

Why continuation passing style

喜你入骨 提交于 2019-12-31 08:58:05
问题 In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons: pass more than one result to its continuation, because the procedure that implements the continuation can take any number of arguments. CPS also allows a procedure to take separate continuations ..., which may accept different numbers of arguments. Since the first reason can also be done using the values procedure and the