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
92
votes
4 answers

How do I use fix, and how does it work?

I was a bit confused by the documentation for fix (although I think I understand what it's supposed to do now), so I looked at the source code. That left me more confused: fix :: (a -> a) -> a fix f = let x = f x in x How exactly does this return…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
17
votes
3 answers

What are the benefits of letrec?

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 defined function operating on arguments that remain…
Ixmatus
  • 1,041
  • 9
  • 15
16
votes
16 answers

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

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…
11
votes
1 answer

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

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…
Dan Burton
  • 51,332
  • 25
  • 109
  • 190
10
votes
2 answers

Transforming a function that computes a fixed point

I have a function which computes a fixed point in terms of iterate: equivalenceClosure :: (Ord a) => Relation a -> Relation a equivalenceClosure = fst . List.head -- "guaranteed" to exist . List.dropWhile (uncurry…
nomen
  • 3,428
  • 1
  • 21
  • 35
8
votes
2 answers

Meaning of letrec in Scheme/Racket

So as far as I understand, the following: let, let*, letrec and letrec* are synthetics sugars used in Scheme/Racket. Now, if I have a simple program: (let ((x 1) (y 2)) (+ x y)) It is translated into: ((lambda (x y) (+ x y)) 1 2) If I…
user4285147
8
votes
1 answer

Why does this code using shadowing `let` bindings hang?

Running this code: j = let x = 4 in let x = x * x in x in the interpreter: ghci> j ... no response ... hangs with very little CPU utilization. Why is this? I expected j = 16.
Matt Fenwick
  • 44,546
  • 19
  • 115
  • 184
7
votes
2 answers

How is "letrec" implemented without using "set!"?

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.
7
votes
3 answers

Sharing vs. non-sharing fixed-point combinator

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) …
5
votes
3 answers

How is it possible that a binding shadows the existing binding in `case of` block?

I have to extract user name and email from Either of AuthResponse. I use case of construct for it: let (uname, uemail) = case getUserResponseJSON creds of Right (GoogleUserResponse uname uemail) -> (uname, uemail) _ ->…
mkUltra
  • 2,382
  • 18
  • 38
5
votes
3 answers

How do I write a function to create a circular version of a list in OCaml?

Its possible to create infinite, circular lists using let rec, without needing to resort to mutable references: let rec xs = 1 :: 0 :: xs ;; But can I use this same technique to write a function that receives a finite list and returns an infinite,…
hugomg
  • 63,082
  • 19
  • 144
  • 230
5
votes
1 answer

What's the difference between R6RS's `letrec`, `letrec*` and Racket's `letrec`?

Both letrec and letrec* are in R6RS, but there's only letrec in Racket, no letrec*. What are the differences between these?
Ben
  • 2,892
  • 3
  • 15
  • 24
5
votes
2 answers

Letrec and reentrant continuations

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…
Lily Chung
  • 2,791
  • 1
  • 22
  • 38
4
votes
3 answers

Haskell `let` bindings in lambda calculus

I want to understand how let bindings work in Haskell (or maybe lambda calculus, if the Haskell implementation differs?) I understand from reading Write you a Haskell that this is valid for a single let binding. let x = y in e == (\x -> e) y This…
4
votes
2 answers

What are the differences between 'let' or 'letrec' and 'define' for creating local bindings?

I don't understand what the differences are between (sorry for the contrived example): (define average (lambda (elems) (define length (lambda (xs) (if (null? xs) 0 (+ 1 (length (cdr xs)))))) (define sum …
Matt Fenwick
  • 44,546
  • 19
  • 115
  • 184
1
2 3