Questions tagged [letrec]

A recursive let construct in e.g. Scheme whereas the "right hand side" can refer to the name being defined. By naming the implicitly defined procedure, lets us reuse it, i.e. call it recursively.

A recursive let construct in Scheme and other functional languages whereas the "right hand side" can refer to the name being defined. By naming the implicitly defined procedure, lets us reuse it, i.e. call it recursively.

38 questions
3
votes
1 answer

Can a local procedure be bound to a variable in letrec without using lambda?

I am a beginner in scheme. I used to do programs like these in using letrec and binding using lambda. (define (drop l n) (letrec ((iter (lambda(ls x) (cond ((null? ls) ls) ((> x 1) (cons (car ls) (iter (cdr ls) (- x 1)))) (else…
Subin Pulari
  • 193
  • 2
  • 11
2
votes
1 answer

Scheme: are `letrec` and `letcc` crucial for efficiency?

I am reading The Seasoned Schemer by Friedman and Felleisen, but I am a little uneasy with some of their best practices. In particular, the authors recommend: using letrec to remove arguments that do not change for recursive applications; using…
J. D.
  • 279
  • 1
  • 9
2
votes
1 answer

How to implement letrec in Javascript?

The following combinator uses default parameters in a creative (some would say abusive) way and it behaves kind of like Scheme's letrec*: * Please correct me if I'm wrong, I don't know Scheme well const bind = f => f(); const main = bind( (x…
user5536315
2
votes
1 answer

Scheme: Why does evaluating this recursive function defined in letrec fail?

I am writing a silly letrec in Scheme (DrRacket Pretty Big): (letrec ((is-creative? (lambda (writing) (if (null? writing) #f (is-creative? (eval writing)))))) (is-creative? (quote…
Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52
2
votes
2 answers

What are the merits of letrec as a programming language feature

I've looked at everything I can find about letrec, and I still don't understand what it brings to a language as a feature. It seems like everything expressible with letrec could just as easily be written as a recursive function. But are there any…
2
votes
2 answers

convert let into lambda in scheme

this is the original form: (define (split-by l p k) (let loop ((low '()) (high '()) (l l)) (cond ((null? l) (k low high)) ((p (car l)) (loop low (cons (car l) high) (cdr l))) …
Fatemeh Rahimi
  • 458
  • 5
  • 20
2
votes
3 answers

confusion of letrec, Scheme

I am struggling the difference between let, letrec, let* ... since scheme is not my primary programming language, my memory is not existing for long time.. I have this function.. now I am very confusing with letrec here.. this is again…
user1915570
  • 386
  • 2
  • 7
  • 27
2
votes
1 answer

Scheme letrec infinite environment

I'm currently writing metacircular evaluator in Scheme, following the SICP book's steps. In the exercise I am asked to implement letrec, which I do in the following way: (define (letrec->let exp) (define (make-unassigned var) (list var…
2
votes
2 answers

Why not letrec as fix?

In the paper Fixing Letrec: A Faithful Yet Efficient Implementation of Scheme’s Recursive Binding Construct by Dybvig et al. it is said that (emphasis mine): A theoretical solution to these problems is to restrict letrec so that its left-hand…
Rhangaun
  • 1,430
  • 2
  • 15
  • 34
1
vote
1 answer

Why TSPL's letrec example can be run in ChezScheme with only let?

In TSPL 3.2 we find: (letrec ((even? (lambda (x) (or (= x 0) (odd? (- x 1))))) (odd? (lambda (x) (and (not (= x 0)) (even? (- x 1)))))) ;; (list (even? 20) (odd?…
wang kai
  • 1,673
  • 3
  • 12
  • 21
1
vote
1 answer

Why is the variable undefined in the initialization form of this letrec?

Scheme complains that in the code below, the x in (+ x 1) is undefined: (letrec ((x 1) (y (+ x 1))) y) Chez Scheme: Exception: attempt to reference undefined variable x MIT Scheme: ;Unassigned variable: x So, I defined another x: (let…
Flux
  • 9,805
  • 5
  • 46
  • 92
1
vote
1 answer

Can every letrec be replaced with letrec*?

For every 100% compliant R7RS-small program that does not rely on any implementation-specific or undefined behavior, is it true that every instance of letrec in the program can be replaced with letrec* without causing any change in behavior? In…
Flux
  • 9,805
  • 5
  • 46
  • 92
1
vote
1 answer

How to use `letrec` to define function "length" within the body of function "lengh-it"

(define length-it (lambda (ls) (length ls 0))) (define length (lambda (ls acc) (if (null? ls) acc (length (cdr ls) (+ acc 1))))) How to use letrec to put the function length within the body of function length-it? The…
1
vote
1 answer

Expression for defining letrec implementing little language in Haskell

I'm writing an evaluator for a little expression language, but I'm stuck on the LetRec construct. This is the language: data Expr = Var Nm | Lam (Nm,Ty) Expr | App Expr Expr | Val Int | Add Expr Expr | If Expr Expr Expr | Let Nm Expr…
1
vote
1 answer

cond with local binding

My question is about rewriting a nested if conditions to a single cond with a branch having a local binding. I am very new to Racket, just making my first steps, so if my question is stupid, please, be lenient. In brief the task is to write a…
Nik O'Lai
  • 3,586
  • 1
  • 15
  • 17