Questions tagged [recursive-datastructures]

A recursive datastructure is a datastructure (e.g. a struct or class) that contains one or several references to instances of the same datastructure as a member.

524 questions
7
votes
1 answer

Haskell Labeled AST: No instance for (Show1 (Label a)), How to construct an instance?

I want to have an annotated AST, so I defined those recursive data structures using Fix: data Term a = Abstraction Name a | Application a a | Variable Name deriving (Read,Show,Eq,Functor,Foldable,Traversable) data Label a b = Label a…
7
votes
3 answers

Insert node at the end of linked list

There is a simple iterative solution for these kind of problems. Node Insert(Node head,int data) { Node newNode = new Node(); newNode.data = data; if (head == null) { return newNode; } Node current = head; while…
CodeYogi
  • 1,352
  • 1
  • 18
  • 41
7
votes
3 answers

A data structure for Logical Expressions in Haskell

I try to create a data structure for working with logical expressions. At first glance, the logical expressions look like Trees, so it seems reasonable to make up it from trees: data Property a = And (Property a) (Property a) | Or …
Sergey Sosnin
  • 1,313
  • 13
  • 30
7
votes
3 answers

Is there an elegant way to have functions return functions of the same type (in a tuple)

I'm using haskell to implement a pattern involving functions that return a value, and themselves (or a function of the same type). Right now I've implemented this like so: newtype R a = R (a , a -> R a) -- some toy functions to demonstrate …
Lily
  • 155
  • 1
  • 6
6
votes
1 answer

How to create a recursive structure in ASP.NET MVC

I have a categories table which has three fields: Id, Title, and ParentId. I'd like to create a recursive hierarchical structure of my table (a tree) in a cshtml file. I'm new to ASP.NET MVC and I don't know how to do that, because there is no…
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
6
votes
2 answers

How can I walk this type with a recursion scheme instead of explicit recursion?

Consider this code: import Data.Maybe (fromMaybe) data MyStructure = Foo Int | Bar String MyStructure | Baz MyStructure MyStructure | Qux Bool Bool MyStructure MyStructure deriving(Eq,Show) makeReplacements :: [(MyStructure, MyStructure)] ->…
6
votes
1 answer

How to define a recursive data type that refers to its definition

I want to write a data type to evaluate the expressions like this: a is evaluated to a a + b * c / d -e is evaluated to a + (b * (c / (d - e))) So the first argument is always a number and the second one is either a number or an expression I…
Ali
  • 97
  • 5
6
votes
2 answers

Recursive struct

Do I need to use typedef in order to build recursive structs? I tried to use the following code without success: struct teste { int data; int data2; struct teste to_teste; };
user1843665
  • 353
  • 2
  • 3
  • 7
5
votes
1 answer

Deep python dictionary recursion

I have a python dictionary: d = { "config": { "application": { "payment": { "dev": { "modes": {"credit,debit,emi": {}}, "company": { "address":…
5
votes
1 answer

How to store recursive datatype with Data.Binary

Data.Binary is great. There is just one question I have. Let's imagine I've got a datatype like this: import Data.Binary data Ref = Ref { refName :: String, refRefs :: [(String, Ref)] } instance Binary Ref where put a = put (refName a)…
Lanbo
  • 15,118
  • 16
  • 70
  • 147
5
votes
3 answers

Recursive data types in Python

What might be the thing in Python that is closest to the to the recursive data types in Haskell? (i.e. using the type's own definition while defining itself.) Edit: To give a more concrete definition of a recursive type, below is a binary tree in…
5
votes
4 answers

How to create recursive tree-like data structure in java using Map?

I have a mind-block when trying to create data-structure that follows the pattern: Map is a main building block and T is either Map or as terminal operator List. Is it possible to build anything similar in Java, this…
Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82
5
votes
2 answers

Recursive variable definitions in Python and F# (probably OCaml, too)

Given these F# type declarations... type Message = | MessageA | MessageB | MessageC | MessageD type State = { Name:string NextStateMap: Map } ...is there an equally expressive definition of this specific…
ttsiodras
  • 10,602
  • 6
  • 55
  • 71
5
votes
1 answer

Haskell AST Annotation with Fix

I am working on creating an AST in Haskell. I want to add different annotations, such as types and location information, so I ended up using fixplate. However, I can't find any examples online and am having some difficulty. I've set up my AST as…
5
votes
2 answers

Recursion schemes using `Fix` on a data-type that's already a Functor?

Still working on my text editor Rasa. At the moment I'm building out the system for tracking viewports/splits (similar to vim splits). It seemed natural to me to represent this structure as a tree: data Dir = Hor | Vert deriving…
1 2
3
34 35