recursive-datastructures

Describe recursive grammar with type aliases

*爱你&永不变心* 提交于 2019-12-24 02:37:07
问题 How can I describe this recursive grammar with type aliases: type FieldValue = Seq[String] :+: String :+: Int :+: Long :+: CNil type FieldLeaf = FieldValue :+: SubField :+: CNil type SubField = Seq[Field] type Field = (String, FieldLeaf) As it stands, the Scala compiler (2.12.1) gives me: Error:(14, 25) illegal cyclic reference involving type FieldLeaf type Field = (String, FieldLeaf) PS the context of this is parsing a recursive grammar with fastparse. Edit (in response to

Parse Directory Structure (Strings) to JSON using PHP

元气小坏坏 提交于 2019-12-21 09:14:57
问题 I have an array of file-path strings like this videos/funny/jelloman.wmv videos/funny/bellydance.flv videos/abc.mp4 videos/june.mp4 videos/cleaver.mp4 audio/uptown.mp3 audio/juicy.mp3 fun.wmv jimmy.wmv herman.wmv End goal is to get them to jsTree. I built a prototype tree from the above sample strings. check it out: http://jsfiddle.net/ecropolis/pAqas/ 回答1: Firstly I would create a recursive function to iterate through your directory into an array function ReadFolderDirectory($dir,$listDir=

Generating all possible trees of depth N?

空扰寡人 提交于 2019-12-18 17:57:13
问题 I have several different types of tree nodes, each of which may have anywhere from 0 to 5 children. I'm trying to figure out an algorithm to generate all possible trees of depth <= N. Any help here? I'm having trouble figuring out how to recursively walk the tree given that each change I make to a node may expose new subtrees (or remove old ones). 回答1: Here's a Python program I wrote up that I think does what you're asking. It'll return all of the possible trees given a starting node.

Confusing […] List in Python: What is it?

纵然是瞬间 提交于 2019-12-17 06:53:31
问题 So I was writing up a simple binary tree in Python and came across [...] I don't believe this to be related to the Ellipsis object, more it seems to have something to do with an infinity loop (due to Python's shallow copy?). The source of this infinity loop and why it doesn't get expanded while expanding when accessed is something I'm completely lost to, however >>> a [[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0] >>> Keys(a)#With a+b [0, 1, 6, 8, 3, -4] >>> Keys

Confusing […] List in Python: What is it?

ε祈祈猫儿з 提交于 2019-12-17 06:53:30
问题 So I was writing up a simple binary tree in Python and came across [...] I don't believe this to be related to the Ellipsis object, more it seems to have something to do with an infinity loop (due to Python's shallow copy?). The source of this infinity loop and why it doesn't get expanded while expanding when accessed is something I'm completely lost to, however >>> a [[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0] >>> Keys(a)#With a+b [0, 1, 6, 8, 3, -4] >>> Keys

Parse recursive data with parsec

偶尔善良 提交于 2019-12-12 12:20:44
问题 import Data.Attoparsec.Text.Lazy import Data.Text.Lazy.Internal (Text) import Data.Text.Lazy (pack) data List a = Nil | Cons a (List a) list :: Text list = pack $ unlines [ "0" , "1" , "2" , "5" ] How can List Int parser coud be implemented to parse Cons 0 (Cons 1 (Cons 2 (Cons 5 Nil))) from list ? ps : pure parser without parsing a [Int] and converting it to List Int is preferable. 回答1: Like this: import Control.Applicative -- rest of imports as in question data List a = Nil | Cons a (List a

Looping through recursive list in C

做~自己de王妃 提交于 2019-12-12 04:37:01
问题 I've just started out with C and I think the whole pointers/malloc/free is driving me mad. I tried to define a simple linear recursive data structure and loop through it, printing each element that I've looped through. (Code as below). However, I'm getting Segmentation Fault: 11 as soon as I try to move to the next element to "insert" a new element #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct number_list { int num; struct number_list *next_num; } numlist; int main

How to write a Show instance for Mu recursive types

好久不见. 提交于 2019-12-11 17:48:19
问题 I want to write an instance of Show for lists of the following type: newtype Mu f = Mu (forall a. (f a -> a) -> a) data ListF a r = Nil | Cons a r deriving (Show) type List a = Mu (ListF a) Module Data.Functor.Foldable defines it, but it converting it to Fix , something I want to avoid. How can I define this Show instance? 回答1: The slogan, "Follow the types!" , works for us here, fulltime. From your code, with some renaming for easier comprehension, {-# LANGUAGE RankNTypes #-} data ListF a r

Recursive collections in Scala such as List

做~自己de王妃 提交于 2019-12-11 14:06:10
问题 The aim is to use a Scala collection where the size needs not be computed iteratively or recursively. For instance List proves to be a recursive construction (consider for instance https://stackoverflow.com/a/8197826/3189923), and in order to obtain the size it is necessary to iterate over it; namely an O(N) operation on the number of elements in the list. Thus to ask for which collections this operation is O(1) ? Many Thanks. 回答1: The cost of method size for main immutable collections

Generate list of parents-childs (recursive relationship) from Dataset

淺唱寂寞╮ 提交于 2019-12-11 09:29:12
问题 I'm trying to build a list (HTML) with a recursive relationship. The data is in a dataset but could converted to a data table if it's easier. I don't know what's the best option to achieve this. I was thinking about using nested repeaters. Here's the data: __ID__ | __NAME__ | __PARENT__ | __LEVEL__ 1 | Patrick | | 1 2 | Mark | | 1 3 | Scott | 2 | 2 4 | Jason | | 1 5 | Julian | | 1 6 | John | 6 | 2 7 | Steve | | 1 8 | George | 1 | 2 9 | Robert | 1 | 2 10 | Rodney | 8 | 3 Here the output I want