tying-the-knot

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

只谈情不闲聊 提交于 2019-12-03 16:07:26
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 = Node b c That strategy is rather elegant, but I couldn't find a way to actually use it without Int labels. For example, how could I write a function that counts the amount of nodes on the square value? countNodes :: Node -> Int countNodes = ... ??? ... main = print $ countNodes square -- output: 4 You do indeed need some kind of labeling, because from inside Haskell

Using Cont to acquire values from the future and the past

帅比萌擦擦* 提交于 2019-12-03 06:56:06
问题 I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again.

Tying the Knot with a State monad

一个人想着一个人 提交于 2019-12-03 02:16:48
问题 I'm working on a Haskell project that involves tying a big knot: I'm parsing a serialized representation of a graph, where each node is at some offset into the file, and may reference another node by its offset. So I need to build up a map from offsets to nodes while parsing, which I can feed back to myself in a do rec block. I have this working, and kinda-sorta-reasonably abstracted into a StateT -esque monad transformer: {-# LANGUAGE DoRec, GeneralizedNewtypeDeriving #-} import qualified

Using Cont to acquire values from the future and the past

旧时模样 提交于 2019-12-02 21:36:00
I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again. A bit more preliminary information. See this version on the github repo for all the details. type

Tying the Knot with a State monad

徘徊边缘 提交于 2019-12-02 17:16:26
I'm working on a Haskell project that involves tying a big knot: I'm parsing a serialized representation of a graph, where each node is at some offset into the file, and may reference another node by its offset. So I need to build up a map from offsets to nodes while parsing, which I can feed back to myself in a do rec block. I have this working, and kinda-sorta-reasonably abstracted into a StateT -esque monad transformer: {-# LANGUAGE DoRec, GeneralizedNewtypeDeriving #-} import qualified Control.Monad.State as S data Knot s = Knot { past :: s, future :: s } newtype RecStateT s m a =

Lazily Tying the Knot for 1 Dimensional Dynamic Programming

谁说胖子不能爱 提交于 2019-11-30 11:19:57
Several years ago I took an algorithms course where we were giving the following problem (or one like it): There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic programming write a function that will compute the number of steps it takes the elevator to get from floor i to floor j . This is obviously easy using a stateful approach, you create an array n elements long and fill it up with the values. You could even use a technically non-stateful approach that involves accumulating a result as recursively passing it

Lazily Tying the Knot for 1 Dimensional Dynamic Programming

落花浮王杯 提交于 2019-11-29 16:58:10
问题 Several years ago I took an algorithms course where we were giving the following problem (or one like it): There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic programming write a function that will compute the number of steps it takes the elevator to get from floor i to floor j . This is obviously easy using a stateful approach, you create an array n elements long and fill it up with the values. You could even use

Explanation of “tying the knot”

白昼怎懂夜的黑 提交于 2019-11-27 11:26:39
In reading Haskell-related stuff I sometimes come across the expression “tying the knot”, I think I understand what it does, but not how . So, are there any good, basic, and simple to understand explanations of this concept? Tying the knot is a solution to the problem of circular data structures. In imperative languages you construct a circular structure by first creating a non-circular structure, and then going back and fixing up the pointers to add the circularity. Say you wanted a two-element circular list with the elements "0" and "1". It would seem impossible to construct because if you

Explanation of “tying the knot”

a 夏天 提交于 2019-11-26 15:34:23
问题 In reading Haskell-related stuff I sometimes come across the expression “tying the knot”, I think I understand what it does, but not how . So, are there any good, basic, and simple to understand explanations of this concept? 回答1: Tying the knot is a solution to the problem of circular data structures. In imperative languages you construct a circular structure by first creating a non-circular structure, and then going back and fixing up the pointers to add the circularity. Say you wanted a two