continuation-passing

Can a convolution function written in tail recursive form?

和自甴很熟 提交于 2019-12-06 03:10:13
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) sum_ways(n_times - 1, k_sum - j, s_side) ) return(sum(sigma_values)) } } I have tried to re-write the

memoize continuation passing style function

谁说胖子不能爱 提交于 2019-12-06 02:41:39
问题 I'm wondering if there is a way to implement a generic "memoize" functional (as in a function with a function as input and a function as output, as python's decorators) capable of handling also cps-style functions. for a normal function (as in "the result value comes back by the return, the parameters are only for input!") a memoize function can be as simple as (in javascript) function memoize(fun) { var cache = {}; return function () { var args = Array.prototype.slice.call(arguments); if

SICP, Continuation Passing Style and Clojure's trampoline

爷,独闯天下 提交于 2019-12-04 19:57:23
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] (letfn [(branch-weight-cps [branch kont] (let [structure (branch-structure branch)] (if (mobile? (branch

Changing a function into CPS style

喜你入骨 提交于 2019-12-04 13:02:42
We were asked to write a procedure that when given a list it will replace the first occurrence of a given element and only the first, but the catch is to write in CPS style. We are unable to turn it to CPS style written procedure that is given a success-cont and fail-cont.. If anyone is willing to give it a try we will really appreciate it :] The procedure we have (graciously provided by answers here ): (define (replace-one list old new) (cond ((pair? list) (let ((next (replace-one (car list) old new))) (cons next (if (equal? next (car list)) ; changed? (replace-one (cdr list) old new) ; no,

Attempting to use continuation passing style to avoid stack overflow with minimax algorithm

大城市里の小女人 提交于 2019-12-04 09:08:06
Summary of my objective: Figure out how to use continuation-passing style to avoid a stack overflow when using an algorithm I believe cannot be made tail-recursive. Alternatively, find a way to make the function tail-recursive. Details: I am new to F# (and functional programming in general) and I am attempting to implement the minimax algorithm with alpha-beta pruning. This is an algorithm used to determine the best possible move for a two-player game. The pseudocode for the algorithm can be found here: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning This is a resource I've found

memoize continuation passing style function

北战南征 提交于 2019-12-04 07:49:49
I'm wondering if there is a way to implement a generic "memoize" functional (as in a function with a function as input and a function as output, as python's decorators) capable of handling also cps-style functions. for a normal function (as in "the result value comes back by the return, the parameters are only for input!") a memoize function can be as simple as (in javascript) function memoize(fun) { var cache = {}; return function () { var args = Array.prototype.slice.call(arguments); if (args in cache) return cache[args]; var ret = fun.apply(this, arguments); cache[args] = ret; return ret; }

Why does the OCaml std lib have so many non-tail-recursive functions?

天涯浪子 提交于 2019-12-04 02:22:06
I have been rewriting many OCaml standard library functions to be tail-recursive lately. Given that this has entailed straight-forward CPS transformation, I am left puzzling over why the default versions are not written this way. As an example, in the standard library, map is defined as: let rec map f = function [] -> [] | a::l -> let r = f a in r :: map f l I have rewritten it to be: let map f l = let rec aux l k = match l with [] -> k [] | a::l -> aux l (fun rest -> k (f a :: rest)) in aux l (fun x -> x) In my experience, tail recursive versions of non-trivial functions often trade space

Is there a way to chain functions like withCString?

谁都会走 提交于 2019-12-04 00:33:45
Is there a way to chain functions like withCString ? By that I mean any function that looks something like f :: Foo -> (CFoo -> IO a) -> IO a . For example, lets say there is a function cFunc :: CString -> CFoo -> CBar -> IO () Usualy, I would do something like: haskellFunc string foo bar = withCString string $ \ cString -> withCFoo foo $ \ cFoo -> withCBar bar $ \ cBar -> cFunc cString cFoo cBar But i would like to do something like: haskellFunc = (withCString |.| withCFoo |.| withCBar) cFunc with some appropriate composition operator |.| . I'm writing library with a lot of C bindings, and

Why continuation passing style

て烟熏妆下的殇ゞ 提交于 2019-12-02 18:08:04
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 second using case-lambda , I'm not clear the advantages of using continuation passing style. Could

[a,b].reduce(f,x) code to [a,b].reduce(f) using transducer /CPS based functional references?

試著忘記壹切 提交于 2019-12-02 09:49:14
问题 In my previous Quesion: Extracting data from a function chain without arrays @Aadit M Shah gave me astonishing solution as follows: https://stackoverflow.com/a/51420884/6440264 Given an expression like A(a)(b)(f) where f is a function, it's impossible to know whether f is supposed to be added to the list or whether it's the reducing function. Hence, I'm going to describe how to write expressions like A(a)(b)(f, x) which is equivalent to [a, b].reduce(f, x) . This allows us to distinguish when