Questions tagged [unfold]

An unfold function is the opposite (or dual) of a fold. Unfolds recursively generate a list (or other recursive data structure) of values from a seed value.

An unfold function is the opposite (or dual) of a . Unfolds recursively generate a list (or other recursive data structure) of values from a seed value.

Unfold functions are often used functional languages which support to construct a whose values are only created on demand.

48 questions
85
votes
9 answers

How to set the default to unfolded when you open a file?

In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?
Suan
  • 34,563
  • 13
  • 47
  • 61
13
votes
2 answers

How to unfold a recursive function just once in Coq

Here is a recursive function all_zero that checks whether all members of a list of natural numbers are zero: Require Import Lists.List. Require Import Basics. Fixpoint all_zero ( l : list nat ) : bool := match l with | nil => true | n :: l'…
user287393
  • 1,221
  • 8
  • 13
12
votes
1 answer

Is a lazy, breadth-first monadic rose tree unfold possible?

Data.Tree includes unfoldTreeM_BF and unfoldForestM_BF functions to construct trees breadth-first using the results of monadic actions. The tree unfolder can be written easily using the forest unfolder, so I'll focus on the latter: unfoldForestM_BF…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
11
votes
1 answer

Efficiency of unfoldr versus zipWith

Over on Code Review, I answered a question about a naive Haskell fizzbuzz solution by suggesting an implementation that iterates forward, avoiding the quadratic cost of the increasing number of primes and discarding modulo division (almost)…
itsbruce
  • 4,825
  • 26
  • 35
9
votes
1 answer

What is the correct definition of `unfold` for an untagged tree?

I've been thinking in how to implement the equivalent of unfold for the following type: data Tree a = Node (Tree a) (Tree a) | Leaf a | Nil It was not immediately obvious since the standard unfold for lists returns a value and the next seed. For…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
7
votes
7 answers

How do I make twig's dump function to display the data unfolded?

I'm using the dump function of the twig. But it shows the data "folded", like in here: When I click the arrow, I may reveal the data by unfolding it, like in here: Question: Is there any way to tell the twig or dump to directly display the objects…
Xavi Montero
  • 9,239
  • 7
  • 57
  • 79
5
votes
2 answers

What is the justification for the type of unfoldr in Haskell?

The example given in the documentation of unfoldr :: (b -> Maybe (a, b)) -> b -> [a]: unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 can easily be written with a redundant pair: unfoldr (\b -> if b == 1 then Nothing else Just (b-1,…
hkBst
  • 2,818
  • 10
  • 29
5
votes
1 answer

typeclass for repetitive actions until fixed point

i noticed a common pattern of executing an action until it stops having certain effects, when one knows that this signifies a fixed point (ie, there can be no future effects). is there a typeclass for this? is this covered by MonadFix? looking at…
4
votes
2 answers

custom unfold returning the accumulator

I'm trying to create a custom unfold function that returns its last accumulator value like: val unfold' : generator:('State -> ('T * 'State) option) -> state:'State -> 'T list * 'State I managed to make the following: let unfold' generator state = …
gbieging
  • 320
  • 1
  • 13
4
votes
1 answer

Does Clojure have "unfold"?

(defn unfold [step seed] (if-let [[val new-seed] (step seed)] (cons val (lazy-seq (unfold step new-seed))) nil)) Example usage: (defn fib-step [[x y]] [x [y (+ x y)]]) (take 10 (unfold fib-step [0 1])) ;=> (0 1 1 2 3 5 8 13 21 34) (defn…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
4
votes
1 answer

Deserializing a nested hash from lists of hash keys

I have a string that I would like to "unflatten" or "tree-ify"; that is, I want to go from this: F=8|A_C=3|A_B=2|D_G_H=11|D_B=2|E=5 to this: { A => { B => 2, C => 3, }, D => { B => 2, G => { H => 11, }, }, …
galvatron
  • 145
  • 8
4
votes
3 answers

Unfold returning the last state of the accumulator

The unfold function in Haskell is very handy to create lists. Its definition is: unfold :: (b -> Maybe (a, b)) -> b -> [a] But I would like to get the last value of the accumulator used. A possible implementation is: unfoldRest :: (b -> Maybe (a,…
PierreBdR
  • 42,120
  • 10
  • 46
  • 62
4
votes
1 answer

Unfoldable instance for the cofree comonad

I'm trying to figure out the difference between unfold/coiter from Control.Comonad.Cofree and unfold/ana from Data.Control.Fixedpoint. Hackage libraries are resp. free and recursion-schemes. Cofree and Fix seem to be cousins, and I'm trying to…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
3
votes
1 answer

Preserve sub-folding on any level when folding/unfolding the mother ones in Sublime Text

I need to preserve the sub-foldings on folding/unfolding the mother (super) ones. when unfold a Class through Ctrl+Shift+]. it will unfold its fold Functions as well. is it possible to Unfold only parent class instead of applying to its sub…
3
votes
0 answers

Haskell - Expressing the Depth First Traversal of a Rose Tree as an instance of unfold, deriving it algebraically

Suppose we have a Rose Tree defined, along with the corresponding fold over the datatype. data RTree a = Node a [RTree a] foldRTree :: (a -> [b] -> b) -> RTree a -> b foldRTree f (Node x xs) = f x (map (foldRTree f) xs) A recursive definition of a…
samlu1999
  • 53
  • 2
1
2 3 4