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.

40 questions
68
votes
8 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
  • 27,428
  • 12
  • 44
  • 60
12
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,171
  • 5
  • 12
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,651
  • 24
  • 34
11
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
  • 44,398
  • 3
  • 56
  • 155
7
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
  • 45,122
  • 42
  • 127
  • 254
5
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
  • 51,332
  • 25
  • 109
  • 190
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
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
  • 7,160
  • 3
  • 38
  • 64
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
  • 300
  • 1
  • 12
4
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
  • 1,291
  • 6
  • 17
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
  • 125
  • 7
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,141
  • 1
  • 39
  • 101
3
votes
2 answers

What is the type of apomorphism specific to list and how is it implemented?

I am learning recursion schemes and it has proven very helpful to me to implement them specific to the list type. However, I got stuck on apomorphisms. Here is an implementation of tails in terms of apo I recently found: import…
scriptum
  • 3,839
  • 1
  • 12
  • 26
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
3
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
  • 38,125
  • 9
  • 41
  • 60
1
2 3