letrec

How is “letrec” implemented without using “set!”?

最后都变了- 提交于 2019-12-22 06:29:35
问题 How can letrec be implemented without using set! ? It seems to me that set! is an imperative programming construct, and that by using it, one loses the benefits of functional programming. 回答1: I know usually we ask content to be copied but there is no short answer to your question. http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf 回答2: No. Just because a functional feature is implemented with imperative code behind the scenes, that doesn't make the feature be imperative. Our computing

How is “letrec” implemented without using “set!”?

て烟熏妆下的殇ゞ 提交于 2019-12-22 06:29:10
问题 How can letrec be implemented without using set! ? It seems to me that set! is an imperative programming construct, and that by using it, one loses the benefits of functional programming. 回答1: I know usually we ask content to be copied but there is no short answer to your question. http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf 回答2: No. Just because a functional feature is implemented with imperative code behind the scenes, that doesn't make the feature be imperative. Our computing

Letrec and reentrant continuations

╄→гoц情女王★ 提交于 2019-12-07 05:31:59
问题 I have been told that the following expression is intended to evaluate to 0, but that many implementations of Scheme evaluate it as 1: (let ((cont #f)) (letrec ((x (call-with-current-continuation (lambda (c) (set! cont c) 0))) (y (call-with-current-continuation (lambda (c) (set! cont c) 0)))) (if cont (let ((c cont)) (set! cont #f) (set! x 1) (set! y 1) (c 0)) (+ x y)))) I must admit that I cannot tell where even to start with this. I understand the basics of continuations and call/cc , but

letrec in Scala? (Immutable way to “Tie the knot?”)

夙愿已清 提交于 2019-12-05 18:02:42
问题 Suppose I have a stupid little case class like so: case class Foo(name: String, other: Foo) How can I define a and b immutably such that a.other is b , and b.other is a ? Does scala provide some way to "tie the knot"? I'd like to do something like this: val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work. Possibilities In Haskell I would do this: data Foo = Foo { name :: String, other :: Foo } a = Foo "a" b b = Foo "b" a Where the bindings to a and b are contained in the same

What are the benefits of letrec?

孤人 提交于 2019-12-05 13:31:21
问题 While reading "The Seasoned Schemer" I've begun to learn about letrec . I understand what it does (can be duplicated with a Y-Combinator) but the book is using it in lieu of recurring on the already define d function operating on arguments that remain static. An example of an old function using the define d function recurring on itself (nothing special): (define (substitute new old l) (cond ((null? l) '()) ((eq? (car l) old) (cons new (substitute new old (cdr l)))) (else (cons (car l)

How is “letrec” implemented without using “set!”?

ε祈祈猫儿з 提交于 2019-12-05 06:07:11
How can letrec be implemented without using set! ? It seems to me that set! is an imperative programming construct, and that by using it, one loses the benefits of functional programming. I know usually we ask content to be copied but there is no short answer to your question. http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf No. Just because a functional feature is implemented with imperative code behind the scenes, that doesn't make the feature be imperative. Our computing machines are all imperative; so at some point all functional code must be implemented by translation into imperative

letrec in Scala? (Immutable way to “Tie the knot?”)

这一生的挚爱 提交于 2019-12-04 02:14:55
Suppose I have a stupid little case class like so: case class Foo(name: String, other: Foo) How can I define a and b immutably such that a.other is b , and b.other is a ? Does scala provide some way to "tie the knot" ? I'd like to do something like this: val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work. Possibilities In Haskell I would do this: data Foo = Foo { name :: String, other :: Foo } a = Foo "a" b b = Foo "b" a Where the bindings to a and b are contained in the same let expression, or at the top level. Or, without abusing Haskell's automagical letrec capabilities: (a

What are the benefits of letrec?

℡╲_俬逩灬. 提交于 2019-12-03 22:52:02
While reading "The Seasoned Schemer" I've begun to learn about letrec . I understand what it does (can be duplicated with a Y-Combinator) but the book is using it in lieu of recurring on the already define d function operating on arguments that remain static. An example of an old function using the define d function recurring on itself (nothing special): (define (substitute new old l) (cond ((null? l) '()) ((eq? (car l) old) (cons new (substitute new old (cdr l)))) (else (cons (car l) (substitute new old (cdr l)))))) Now for an example of that same function but using letrec : (define

Which languages support *recursive* function literals / anonymous functions?

假装没事ソ 提交于 2019-12-03 08:54:00
问题 It seems quite a few mainstream languages support function literals these days. They are also called anonymous functions, but I don't care if they have a name. The important thing is that a function literal is an expression which yields a function which hasn't already been defined elsewhere, so for example in C, &printf doesn't count. EDIT to add: if you have a genuine function literal expression <exp> , you should be able to pass it to a function f(<exp>) or immediately apply it to an

Sharing vs. non-sharing fixed-point combinator

删除回忆录丶 提交于 2019-11-30 09:27:51
This is the usual definition of the fixed-point combinator in Haskell: fix :: (a -> a) -> a fix f = let x = f x in x On https://wiki.haskell.org/Prime_numbers , they define a different fixed-point combinator: _Y :: (t -> t) -> t _Y g = g (_Y g) -- multistage, non-sharing, g (g (g (g ...))) -- g (let x = g x in x) -- two g stages, sharing _Y is a non-sharing fixpoint combinator, here arranging for a recursive "telescoping" multistage primes production (a tower of producers). What exactly does this mean? What is "sharing" vs. "non-sharing" in that context? How does _Y differ from fix ? "Sharing"