tying-the-knot

Mutually recursive evaluator in Haskell

吃可爱长大的小学妹 提交于 2021-02-10 04:55:29
问题 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 = String type Binds = [(Var, Expr)] data Expr = Var Var | Lam Var Expr | App Expr Expr | Con Int | Sub Expr Expr | If Expr Expr Expr | Let Var Expr Expr | LetRec Binds Expr deriving (Show, Eq) And this this the evaluator so far: data Value = ValInt Int

Patching a recursively-defined list without a <<loop>>

六眼飞鱼酱① 提交于 2019-12-22 05:08:25
问题 Context We all know the recursively-defined Fibonacci sequence: fibs = 1 : 1 : zipWith (+) fibs (tail fibs) λ> fibs [1,1,2,3,5,9,13,21,34,55,89... Question I'm trying to “patch” it in a few places, so that: the general recursive equation “element is sum of two previous elements” holds, but there can be countable exceptions as to individual elements' values. Where I'm at Utility To this end, I'll define the following function to modify a specific element in a list: patch :: Int -> a -> [a] ->

Patching a recursively-defined list without a <<loop>>

穿精又带淫゛_ 提交于 2019-12-22 05:08:06
问题 Context We all know the recursively-defined Fibonacci sequence: fibs = 1 : 1 : zipWith (+) fibs (tail fibs) λ> fibs [1,1,2,3,5,9,13,21,34,55,89... Question I'm trying to “patch” it in a few places, so that: the general recursive equation “element is sum of two previous elements” holds, but there can be countable exceptions as to individual elements' values. Where I'm at Utility To this end, I'll define the following function to modify a specific element in a list: patch :: Int -> a -> [a] ->

Self-reference in data structure – Checking for equality

只谈情不闲聊 提交于 2019-12-13 12:11:02
问题 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 Point is created that has itself as its parent (I believe this is called tying the knot ): makeSet' :: a -> Point a makeSet' x = let p = Point x p 0 in p Now, when I wanted to write findSet (i.e. follow parent pointers until you find a Point whose parent is

Birecursively defining a doubly infinite list of lists

微笑、不失礼 提交于 2019-12-10 20:18:45
问题 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 one: pascals = repeat 1 : map (scanl1 (+)) pascals [1,1,1,1,1,1... [1,2,3,4,5... [1,3,6,10... [1,4,10... [1,5... [1... Question I'd like to express it such that: I'll come with my own first rows and columns (example above assumes first row is repeat 1 , which

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

时间秒杀一切 提交于 2019-12-09 12:57:50
问题 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 $

Equational reasoning with tying the knot

◇◆丶佛笑我妖孽 提交于 2019-12-08 17:28:16
问题 I'm trying to wrap my head around Cont and callCC, by reducing this function: s0 = (flip runContT) return $ do (k, n) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ print n if n < 3 then k (n+1) >> return () else return () I've managed to reach this point: s21 = runContT (let f x = ContT $ \_ -> cc (f, x) in ContT ($(f,0))) cc where cc = (\(k,n) -> let iff = if n < 3 then k (n+1) else ContT ($()) in print n >> runContT iff (\_ -> return ())) And at this point i have no idea what

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

夙愿已清 提交于 2019-12-05 18:02:42
问题 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 this: val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work. Possibilities In Haskell I would do this: data Foo = Foo { name :: String, other :: Foo } a = Foo "a" b b = Foo "b" a Where the bindings to a and b are contained in the same

Self-reference in data structure – Checking for equality

孤人 提交于 2019-12-04 14:37:53
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 Point is created that has itself as its parent (I believe this is called tying the knot ): makeSet' :: a -> Point a makeSet' x = let p = Point x p 0 in p Now, when I wanted to write findSet (i.e. follow parent pointers until you find a Point whose parent is itself) I hit a problem: Is it possible to check if this is the case? A naïve Eq instance would of course

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

这一生的挚爱 提交于 2019-12-04 02:14:55
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 this: val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work. Possibilities In Haskell I would do this: data Foo = Foo { name :: String, other :: Foo } a = Foo "a" b b = Foo "b" a Where the bindings to a and b are contained in the same let expression, or at the top level. Or, without abusing Haskell's automagical letrec capabilities: (a