1,179 questions
Best practices
0
votes
1
replies
107
views
How can I simplify my Foldable instance for a rose tree?
I currently have this implementation:
data RoseTree a = RoseTree a [RoseTree a]
preorder :: Tree a -> [a]
preorder (Tree x ts) = x : concatMap preorder ts
instance Foldable Tree where
foldr :: (...
Best practices
2
votes
6
replies
117
views
Short circuit F# fold
Is there a convenient way to short circuit a Seq.fold evaluation?
For example, the following function evaluates if parenthesis are balanced in a string.
let isBalanced str =
(([], str) ||> Seq....
0
votes
1
answer
83
views
Haskell Foldr ADT from a List of Expressions into a single expression
So this is the data definition and my function.
Task 3
Define addAll, which adds a list of expressions together into a single expression without introducing any 'junk'.
You could use foldr since it ...
2
votes
1
answer
150
views
How do I define recr using foldr in Haskell?
This is from a guide of exercises that focuses on structural recursion (foldr), primitive recursion (recr) and iterative recursion (foldl) in Haskell.
The suggestion for this excercise is to return a ...
0
votes
2
answers
92
views
How to write a fold method for an iterator in a language without mutability?
Suppose you had a programming language in which all variables are immutable. Modifying iterables could be accomplished by providing a magic variable that is immutable during an iteration step, but ...
1
vote
2
answers
109
views
Implementing a factorial function in Scala
I'm working out of Scala for the Impatient (3rd Edition). (I'm not doing this for a class.). I'm trying to implement a factorial function using to and reduceLeft without a loop or recursion. This ...
0
votes
2
answers
250
views
Why does `foldl'` have the implicit reversal?
Recently to better understand the advantages and disadvantages of lazy evaluation, I check for some details of Haskell. I did that to do exercise 5.9 in SDF (Software Design for Flexibility)
More ...
4
votes
3
answers
160
views
How can I use parameter pack and template function to differentiate unnamed structs?
I have written a C++20 concept myrequirement:
template<typename T>
concept myrequirement= requires(T s)
{
{ s.type() } -> std::same_as<uint8_t>;
};
enum MyType
{
TYPE1,
...
2
votes
1
answer
98
views
Unpacking member type from std::tuple types
I have a collection of struts, each with a data_t member type (other members omitted):
struct A { using data_t = int; };
struct B { using data_t = double; };
struct C { using data_t = bool; };
// etc.
...
8
votes
4
answers
420
views
Can I write a dependent left fold in terms of a dependent right fold?
Suppose we have
data Nat = Z | S Nat
type Vec :: Nat -> Type -> Type
data Vec n a where
Nil :: Vec Z a
(:::) :: Vec n a -> Vec (S n) a
infixr 4 :::
deriving instance Foldable (Vec n)
...
3
votes
2
answers
121
views
F# foldBack conversion to tail recursive function
For learning purposes I've emulated List.foldBack. The emulated version is head-recursive.
Is there an elegant way to convert it to the tail-recursive version?
let li : int list = [1;2;3]
let rec ...
0
votes
0
answers
41
views
Is there a way to fold all sections of code found by a search in Visual Studio Code?
I would like to fold all JS doc comments within my code. I just found that folding all comments can be done with Ctrl+K Ctrl+/. Yet I am still wondering if there is a way to fold all occurrences in ...
6
votes
2
answers
151
views
substitution in proofs with recursive formulas
The following problem and partial solution are from Richard Bird's Thinking Functionally with Haskell (pp 132-133, 139)
given
foldl f e (x:xs) = foldl f (f e x) xs
foldl f e [] = e
Prove foldl (@) e ...
0
votes
1
answer
104
views
One way to construct graph adjacency list based on adjacency pairs similar to `fold` in MIT Scheme
Recently when I self-learnt MIT 6.5151 course, I first read CS 61AS Unit 0 as the preparation. Then I have read SICP 1 to 2.1 (with related lecture notes) as ps0 requires (also read 2.2.1 as CS 61A ...
-1
votes
1
answer
195
views
how do I reenable folding arrows in the ruler
I use vscode.
Foldable code blocks used to have fold/unfold arrows in the ruler, now however, we have the sticky scroll.
Is there a way to re-enable the fold arrows in the ruler?