Questions tagged [tying-the-knot]

Tying the knot is a technique in which you can create circular data structures in the absence of mutation by referencing a yet to be produced value.

28 questions
5
votes
3 answers

Is it possible to do a search on a graph constructed with the tying-the-knot strategy?

The tying-the-knot strategy can be used to construct graphs such as, using a simple two-edged graph as an example: data Node = Node Node Node -- a - b -- | | -- c - d square = a where a = Node b c b = Node a d c = Node a d d =…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
4
votes
1 answer

Debugging and understanding "tying the knot" in a monadic context

I'm trying to implement an interpreter for a programming language with lazy-binding in Haskell. I'm using the tying-the-knot pattern to implement the evaluation of expressions. However I found it extremely hard to debug and to reason about. I spent…
Blue Nebula
  • 932
  • 4
  • 9
4
votes
2 answers

How can I avoid <> in Haskell?

The program below results in <> in GHC. ...Obviously. In hindsight. It happens because walk is computing a fixed point, but there are multiple possible fixed points. When the list comprehension reaches the end of the graph-walk, it "asks" for…
Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
4
votes
1 answer

Functional Pearl: Implementing trace in JavaScript

Ross Paterson: Arrows and Computation introduces the trace function (on page 11): trace :: ((a, c) -> (b, c)) -> a -> b trace f a = let (b, c) = f (a, c) in b The trace function is useful for modularizing the magic feedback step in circular…
3
votes
1 answer

Level-order repminPrint

repmin problem is pretty well-known. We are given a data type for trees: data Tree a = Leaf a | Fork (Tree a) a (Tree a) deriving Show We need to write a function down (repmin) which would take a tree of numbers and replace all numbers in it by…
3
votes
2 answers

Birecursively defining a doubly infinite list of lists

Context I asked about patching a recursively-defined list the other day. I'm now trying to bring it up a level by operating on a 2D list instead (a list of lists). I'll use Pascal's triangle as an example, like for example this beautiful…
JB.
  • 40,344
  • 12
  • 79
  • 106
3
votes
2 answers

Tie-the-knot in 2 dimensions (was: tying the knot with a comonad)

Edit: The original question was "tying the knot with a comonad", but what really helped here is a two-dimensional knot tying with U2Graph from cirdec. Original question (until Anwser): I want to tie the knot with data that originates from a…
Franky
  • 2,339
  • 2
  • 18
  • 27
3
votes
4 answers

Self-reference in data structure – Checking for equality

In my initial attempt at creating a disjoint set data structure I created a Point data type with a parent pointer to another Point: data Point a = Point { _value :: a , _parent :: Point a , _rank :: Int } To create a singleton set, a…
beta
  • 2,380
  • 21
  • 38
2
votes
3 answers

Mutually recursive evaluator in Haskell

Update: I've added an answer that describes my final solution (hint: the single Expr data type wasn't sufficient). I'm writing an evaluator for a little expression language, but I'm stuck on the LetRec construct. This is the language: type Var =…
Tom Lokhorst
  • 13,658
  • 5
  • 55
  • 71
1
vote
1 answer

How does repmin place values in the tree in Haskell?

I really like the repmin problem: Write down repmin :: Tree Int -> Tree Int, which replaces all the numbers in the tree by their minimum in a single pass. If I were writing something like this in python, I would go for passing values by their…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
1
vote
0 answers

Haskell: handling cyclic dependencies while tying the knot

While writing a programming language that will feature local type inference (i.e. it will be capable of inferring types with the exception of function parameters, like Scala), I've run into a problem with cyclic dependencies. I perform…
0
votes
1 answer

How to perform Tying the Knot/define observable recursively using iteself in Rx.Net?

Sometimes the business logic seems to be able to naturally modeled by some recursive defined observables. Here is one example: interface Demo { IObservable userCommands; IObservable> processes; …
xiang0x48
  • 621
  • 6
  • 20
0
votes
1 answer

Non-exhaustive patterns in aux function for tying the knot

I am trying to write a function in Haskell that takes a table and pads up cells of each column according to the maximum size of a string in that column. The way I am doing this is by using the technique - tying the knot. Here is the function that I…
Iguana
  • 247
  • 1
  • 8
1
2