19,239 questions
0
votes
0
answers
44
views
Mutually recursive stack-safe functions with different types
I want to make my mutually recursive functions stack-safe, but because they have different signatures - one traverses a list and other a tree(?) of sorts - I'm not clear how to go about this.
Here's ...
1
vote
2
answers
113
views
Replacing the element in the list with the specific index using `foldr` —— Exercise 1.19 of Essentials of Programming Language
Here is the question from Mitchell Wand's Essentials of Programming Language:
Exercise 1.19 [⋆ ⋆] (list-set lst n x) returns a list like lst, except that the n-th element, using zero-based indexing, ...
Advice
0
votes
1
replies
78
views
How to effectively use ASP.NET core Hybrid Cache with Result<T> type
Suppose a scenario where:
you need to execute an expensive operation which can fail or succeed.
the result of the operation is modeled by using a result object Result<T>. The result object ...
Best practices
5
votes
3
replies
149
views
Is there a way to tuck in Ord inside Applicative?
I had a nice idea of using applicative for nondeterministic financial modelling. Or maybe it is a simple case of sentization. So the basic example is to define newtype ValueRange.
newtype ValueRange a ...
0
votes
1
answer
91
views
How to split a shell command into tokens in a quote-aware fashion? [closed]
The task
Given the following string¹
one ' two 'three four
the required function should split it in 3 tokens (one, two three, and four), in agreement with how the bash shell does:
$ function ...
0
votes
1
answer
124
views
What's the idiomatic/correct way to split a string into its first word and the rest of the string? [closed]
tl;dr
Ideally, I'd want the following
"hello world" is split in "hello" and " world"
" world" is split in "" and " world"
"hello "...
4
votes
1
answer
115
views
Understanding usage of withFileBlocking with named pipes
The following program
assumes that /path/to/mypipe is a named pipe, e.g. created via mkfifo /path/to/mypipe, with no readers/writers waiting yet,
runs two threads, of which
the main thread keeps ...
3
votes
2
answers
151
views
Functional core - Imperative shell: dealing with logic dependant on conditional & expensive IO operations
tldr; how does one deal with logic that depends on data that are too heavy to fetch up-front when subscribing to the functional core, imperative shell line of thought?
Years ago I was inspired by Gary ...
3
votes
2
answers
104
views
Why doesn't readFile block on unix pipe in which no write has happened yet?
If in a terminal I enter
mkfifo /tmp/pipe
echo hello > /tmp/pipe
(which blocks) and in another I run the haskell program
main = readFile "/tmp/foobar" >>= putStr
then I see it ...
2
votes
1
answer
86
views
What does it mean that the arguments to <*> and their associated effects are known statically?
I'm reading the paper Selective Applicative Functors. So far I've read from page 16 out 29, and I think I've understood the gist of this abstraction, but I'm having some trouble with some basic ...
1
vote
2
answers
237
views
How can I transform large nested PHP arrays immutably without creating expensive deep copies?
I’m working with large nested associative arrays in PHP and I need to apply transformations (like map, filter, reshape) immutably, meaning the original array should not be modified.
The problem is ...
1
vote
0
answers
120
views
How to parametrize a loop body with a function while guaranteeing performance?
Consider the following toy code which performs very simple (a few instructions) operations on a hot loop over a large amount of values:
template<float (*f)(float)>
void process_f(int count, ...
0
votes
1
answer
59
views
Haskell Parsec Parser giving strange error
I am having trouble getting the following code to work. I got it to work originally, then I changed it so all the Nonterminals also have positions. And I added the method parseLexeme that transforms ...
1
vote
1
answer
108
views
How can I avoid declaring full generic parameters when storing a custom typed object, while keeping type safety at compile time?
I've created a small library called StreamX, which acts like a type-safe, index-aware zipper to enable parallel streaming over multiple lists. It supports functional operations like forEach, map, ...
7
votes
2
answers
206
views
What is wrong with this closed type family in GHC2021-GHC2024 as compared to Haskell2010?
In Haskell in Depth, Chapter 11, there's an example aimed at avoiding character escaping in GHCi, which is what happens automatically when you enter print "ë", as it'll be printed like "...
0
votes
1
answer
124
views
How Can I Make an Infinite Already Sorted Heap in Haskell?
I start with several containers of things:
[1,2,3]
fromList [4, 42, 77]
Array Vertex [99, 44, 33]
-- The specific containers are just examples. It would be nice to not assume
-- they are Ord instances,...
0
votes
1
answer
85
views
What is a Nothing->Nothing function called in FP?
I am a beginner to functional programming where most of my experience comes from Java/Kotlin. From my understanding functional programming has special names for certain function signatures.
Type ->...
0
votes
2
answers
84
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 ...
0
votes
2
answers
80
views
conditional map fetch efficiently
I have a map which lays down prices of commodities in different currencies
val commpricemap: Map[String , Map[String, Double]] = ???
AN example of an entry for gold is as below:
("AU" -> ...
1
vote
1
answer
111
views
How to implement interrupts in pure functional languages [closed]
How would a pure functional programming language with encapsulated effects via monads (e.g. haskell) deal with interrupts?
Usually, interrupt handlers modify some global state, but accessing a global ...
4
votes
1
answer
98
views
Why aren't all instances of Data.Vector.Fixed.Vector also instances of Functor?
For Vec2, Vec3, and others from Data.Vector.Fixed.Boxed, I see a Functor instance, so something like this type-checks:
fff :: B.Vec2 Int -> B.Vec2 Int
fff = fmap (+1)
and fmap is just Data.Vector....
0
votes
0
answers
55
views
Any way to make this type-aligned sequences constructor infer types properly?
I am in the process of trying to implement Reflection Without Remorse in Typescript, as an exercise, and in the process I'm implementing a bit of a standard library for functional programming.
For ...
0
votes
0
answers
56
views
Is there a way to make a suspension type that is stack safe in a strict language?
Suppose I were trying to build lazy datastructures in a lazy language (Im currently using Typescript, but the specific language matters little).
The obvious implementation is
class Lazy<T> {
...
-1
votes
3
answers
116
views
How do I use fixed-size arrays and extract entries by index?
What package should I go to if I want to have fixed-size array like C++'s std::array?
I've seen at least two packages exist:
vector-sized
fixed-vector
I've started playing a bit with the latter, but ...
0
votes
3
answers
99
views
Partial Function Inheritence in Python?
I have two files in a library I am creating:
file1.py
def func1(a, b):
c = a + b
return c
file2.py
def func2(a, b):
c = a - b
return c
Now I want each file to have a function that ...
1
vote
2
answers
104
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 ...
1
vote
3
answers
120
views
Optimizing linear search in Scala
I implemented a following algorithm:
val acceptedTopics: Set[String] = ...
val arr: List[JValue] = ... // JValue is an algebraic trait extended by JString, JInt, and other types.
val topics = arr....
1
vote
2
answers
93
views
Filter an optional sequence
I have a Option sequence of dates - from which I need to SAFELY extract the first date after a given date( or else return the given date).
The below seems to be a lot of code for such a simple used ...
2
votes
1
answer
54
views
Returning the type of a constructor by a function
I am trying to write a simple tuple type
open import Agda.Builtin.List
mutual
f : List Set -> Set
f T = g T T where
g : List Set -> List Set -> Set
g [] T = Tuple T
g (t ∷ ...
0
votes
1
answer
113
views
Is there a way to obtain an accurate description of the heap state?
I was fascinated by this video on visualization of Haskell heap, but unfortunately I haven't been able to use ghc-vis. My understanding is that it is too old now. (Feel free to suggest how to actually ...
0
votes
2
answers
249
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 ...
0
votes
1
answer
53
views
Unsure of why the helper function doesn't render in elm [closed]
So I'm doing a bit of tweaking with elm to become familiar with it, but when trying to make functions for handling the different views of the application, there's a rounte that doesn't work and I can'...
0
votes
0
answers
63
views
Is it feasible to solve DMOJ's "Tree Tasks" problem using Lean 4?
I'm attempting to solve the DMOJ problem Tree Tasks(https://dmoj.ca/problem/treepractice1), which involves computing the diameter and radius of a weighted tree. My goal is to implement a solution in ...
3
votes
2
answers
191
views
Why doesn't MonadMaybe exist?
The way I look at MonadState, for instance, is that any type (or set of types, e.g. ReaderT r m a) that implements it, must support get+put (or alternatively just state) in order to behave like the ...
0
votes
3
answers
117
views
What is the difference between being "short-circuiting" and being "lazy on the its second argument" for a binary function?
In the Examples section of foldr's documentation, I read that
Infinite structures
⚠️ Applying foldr to infinite structures usually doesn't terminate.
It may still terminate under one of the following ...
2
votes
1
answer
59
views
Is "Associated Types with Class" known to have some typos in it? Or are associated types far different today than what the paper describes?
I'm reading Associated Types with Class, and I'm running into code that doesn't seem to be compilable. Is it just a typo in the paper, or is that things have changed a lot since 2004, when the paper ...
6
votes
1
answer
64
views
Is there a way to have ghci print "Type" instead of "*" in kind signatures?
If I do
λ> :k Monad
at the ghci prompt, I get
Monad :: (* -> *) -> Constraint
Out of curiosity, is there a way to have it print Type instead of *?
I am reading Thinking with Types, and I've ...
0
votes
0
answers
103
views
How should we define and compare specificity in subtyping for higher-rank polymorphic function types?
In systems that support higher-rank polymorphism (such as Peyton Jones et al.'s "Practical Type Inference for Arbitrary-Rank Types"), I’m trying to understand how to reason about specificity ...
-1
votes
2
answers
77
views
Why is this lemma not automatically proved in dafny? How to prove it by hand?
In simple words:
If A and B are equivalent multisets, then elements not in A are not in B as well.
lemma ElementExclusionLemma<T>(seqa: seq<T>, seqb: seq<T>, x: T)
ensures multiset(...
1
vote
2
answers
51
views
Dafny verifier fails to prove the consequence of multiset
I'm working with Dafny and trying to understand how specifications, particularly modifies and ensures multiset, affect verification.
I have a method do_nothing which is specified to modify an array a ...
2
votes
1
answer
126
views
Functional programming with data.table
I want to make a simple function in R that works with data.frames/tibbles or with a data.table. So I created a new method:
pesquisarComposicao <- function(BASE, ...) {
UseMethod("...
1
vote
3
answers
181
views
In the context of STM, what does a transaction log conceptually look like, and how does it evolve when the transaction succeeds after a few retries?
For instance consider this function, that could be used in a WM to allow moving a window from one desktop to another on a given display,
moveWindowSTM :: Display -> Window -> Desktop -> ...
3
votes
1
answer
86
views
How do let and case differ in terms of lazyness?
How do let and case differ in terms of lazyness? Where do I find the information myself? And how do I experiment with it in ghci?
Some context
This incomplete piece of code is an excerpt from
Parallel ...
0
votes
2
answers
60
views
How do I pass an updated object down to a Child Component in React?
I have this code where I've updated an object in React. When you click "Most upvoted", it takes an object, sorts it by votes, and then updates the state with it. The problem is, it won't ...
2
votes
1
answer
80
views
How can GHC's fairness guarantee not show up if a thread is descheduled while it is not holding the MVar?
In Parallel and Concurrent Programming in Haskell by Simon Marlow, chapter 7 starts at page 125 with this example,
import Control.Concurrent
import Control.Monad
import System.IO
main :: IO ()
main = ...
1
vote
2
answers
82
views
Clarification on the importance of the immutability of the state inside an MVar in the context of concurrency
In Parallel and Concurrent Programming in Haskell by Simon Marlow, at pages 133 and 134 the following code is shown:
type Name = String
type PhoneNumber = String
type PhoneBook = Map Name ...
2
votes
1
answer
106
views
Why should bracket's first argument perform at most one blocking operation?
In Parallel and Concurrent Programming in Haskell by Simon Marlow, the implementation of bracket is shown,
bracket
:: IO a -- ^ computation to run first (\"acquire resource\")...
3
votes
3
answers
118
views
Does throw behave the same as throwIO, if instantiated as IO a?
I'm reading Parallel and Concurrent Programming in Haskell by Simon Marlow, and I initially read this suggestion fairly lightly,
It is always better to use throwIO rather than throw in the IO monad ...
2
votes
1
answer
178
views
Lean 4: Agda user struggling to understand Lean's equality type, type mismatch, not reducing
I am quite comfortable with Agda. I decided to experiment with Lean, but I find that propositional equality is really messing with me. Sometimes I find that rfl just works, but at other times it doesn'...
2
votes
0
answers
144
views
Is it possible to create true sentinel values in Rust?
Say I have some simple struct, eg:
struct SomeStruct {
things: Vec<usize>
recent_thing: Option<usize>
}
I'm trying to assert some invariant on this struct. For example, maybe we want ...