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.

32 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 P
  • 163
  • 9
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.
  • 257
  • 1
  • 8
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
  • 16,864
  • 8
  • 33
  • 50
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
  • 466
  • 5
  • 18
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,262
  • 2
  • 14
  • 34
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
0 answers

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…
scriptum
  • 3,839
  • 1
  • 12
  • 26
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,077
  • 1
  • 12
  • 16
0
votes
0 answers

Generalised letrec semantics for mutual recursion

I'm new to system types and I was wondering how mutual recursion is defined through generalized let rec x1=e1 ,....,xn=en in e .What has to be added in the "simple" letrec's semantics and evaluation rules and how is its syntactic sugar (using fix )…
0
votes
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…
0
votes
2 answers

Understanding recurive let expression in lambda calculus with Haskell, OCaml and nix language

I'm trying to understand how recursive set operate internally by comparing similar feature in another functional programming languages and concepts. I can find it in wiki. In that, I need to know Y combinator, fixed point. I can get it briefly in…
varvir
  • 65
  • 7
0
votes
2 answers

how to pattern match 'letrec'

I am trying to pattern match calls to letrec using match-lambda. It seems to me that this pattern: (match-lambda (`(letrec ((, ,) . (, ,)) , . ,) `()) should match calls of the form: (letrec ((
Schemer
  • 1,495
  • 3
  • 16
  • 35
0
votes
2 answers

Ocaml, implement freevars letrec using sets

I'm trying to implement letrec using mathematical lambda notation for the function, but I'm having difficulty. My assignment says that let can be defined as p(e1) U (p(e2) - {x}) and that letrec can be defined as (p(e1) - {f x}) U (p(e2) - {f})…