Questions tagged [haskell]
A functional programming language
260 questions
0
votes
0
answers
727
views
Haskell where clause: is it more than just a matter of taste?
The traditional (Scheme, Pascal) way to structure code is this:
declare outer function
declare inner function
body of inner function
body of outer function
The where clause in Haskell moves ...
1
vote
0
answers
84
views
Haskell: Data Modeling an Internal App that Interfaces with a 3rd-Party Library?
I wrote a java call graph generator with tree sitter (the 3rd-party library in question)'s python binding. I am doing a thought experiment on how I might model the same application in Haskell. I am ...
1
vote
1
answer
367
views
In what sense are functions like fst and snd determined uniquely by their type signature in Haskell?
I have recently given Haskell another go, mostly because I heard about the book Haskell from first principles and so far I'm having a blast. My background is that of a mathematician mostly working in ...
6
votes
1
answer
530
views
Terse finite state machines in Haskell
I'm writing a parser for a markup language in haskell, and a finite state machine fell out of my ideal API. I have code that looks a bit like this:
Token = BoldWord String | Word String | ...
31
votes
3
answers
6k
views
Definition of "functor"; Haskell vs. C++
I'm trying to understand whether the Haskell and C++ communities mean different things by the word "functor", or if there's some underlying concept that unifies the two meanings.
My ...
0
votes
2
answers
239
views
Is a safe function returning Maybe partial or total?
The Elm Guide says to use Maybe for partial functions, but I was under the impression that returning Maybe solves the problem of partial functions and makes them total. It gives a value from the ...
0
votes
1
answer
184
views
Imperative parallels to Haskell's Monad operations
Would it be (mostly) correct to say that the following are the parallels to the Haskell Monad operations in the imperative world?
Monad's >> ~ C/C++/JavaScript/etc. , operator
do expressions ~ C/...
9
votes
4
answers
5k
views
Functional programming - what to learn and who uses it
I'm a .Net and Angular developer who's been working with OO languages throughout my education and work history. Lately I've been thinking about spending some time with one of the functional ...
0
votes
1
answer
258
views
How do Haskell Lists Desugar?
So, I was wondering about how Haskell's lists are implemented. I looked it up, and found this:
data [] a = [] | a : [a]
So I get that you can write lists like this if you want to:
a:b:c:[] -- ...
3
votes
3
answers
269
views
Precisely define "what to solve" and "how to solve" corollary in functional and imperative programming respectively
I am not sure if I ever clearly understood standard corollary "what to solve" and "how to solve" used to point out difference between functional (declarative) and imperative programming paradigm ...
-3
votes
2
answers
178
views
How can we handle inserts/delete cases using Map like data structures in FP?
Let's say, that we keep track of students entering the auditorium using their IDs(Key) and their check-in time(Value) in a Map. We delete the entries once they move out of the auditorium. I want to ...
6
votes
1
answer
763
views
What's the benefit of avoiding partial functions in Haskell?
AFAIK in Haskell it is heavily recommended to avoid partial functions; and if these seem unavoidable (eg head) then return a Maybe. At least, so the Haskell wiki says 1 2
What's the use of the ...
2
votes
1
answer
341
views
Placing Haskell typeclass instances
I'd like to hear some pros and cons about where it's best to put Haskell typeclass instances. I identify 2 possible cases and can not decide for myself which one is best:
Put the instances together ...
0
votes
3
answers
744
views
In which way Haskell limit side effects?
From what I gathered from learning Haskell, functional programming limits the amount of side effects, but in what ways? Hope someone can enlighten me on this one.
1
vote
4
answers
517
views
In Haskell, is it a "violation" of functional programming to interact with something that was not a function parameter?
I'm sure this must have been asked before, but I can't find anywhere that actually answers my question, so apologies if I have simply overlooked this.
I am currently learning Haskell, and loving the ...
0
votes
1
answer
221
views
In what other locations besides infinite streams and infinite lists is memoized lazyness useful?
Haskell is one of the few non-strict languages out there.
In his paper Why Functional Programming Matters, John Hughes uses (memoized) lazy evaluation (as well as higher-order functions) to implement ...
2
votes
2
answers
2k
views
Patterns for tracking state in recursive Haskell code
A frequent pattern in my Haskell code is element-wise recursion for transformation of a list with some carried state generated using the data in the list. Usually, this looks something like this:
...
1
vote
1
answer
259
views
Improving my inelegant Haskell deduplication function
I'm working on learning Haskell, and one of the simple exercises I put myself through to this end is writing a function that deduplicates a list, removing all of the duplicate elements of a list such ...
12
votes
1
answer
396
views
Why does GHC represent recursion in Haskell with recursive binders instead of a fixpoint operator?
GHC's Core data type represents recursion with recursive binders in the Let constructor; as I understand it, all let expressions in Haskell are effectively let rec expressions. Why does GHC use this ...
2
votes
2
answers
229
views
Is writing arithmetic functions to allow "type coercion" a good idea?
Let's say I want to write a function to compute factorials of nonnegative integers. I could write something like this:
fact :: Num a => Int -> a
fact n = fromInteger(product [1..n])
(Let's not ...
2
votes
1
answer
161
views
Dynamic *dll substitution?
I got an architectural problem here.
Let say there is an IShell. It mainly responsible to map user's commands (represented as a linux-like strings) to an appropriate IExecutable's.
Those ...
6
votes
3
answers
435
views
Can referential transparency be assumed when dealing with floating-point arithmetic?
A pure function is assumed to produce the same outputs given the same inputs. Suppose an (otherwise) side-effects free function computes with floating-point numbers. Due to numerical error, these ...
8
votes
1
answer
1k
views
How does one reason about algorithmic complexity in Haskell?
In Haskell, lazy evaluation can often be used to perform efficient calculations of expressions that are written in a clear and concise manner. However, it seems that the language itself does not ...
2
votes
4
answers
841
views
What is the benefit of short readable code if you only see functions and classes on the outside?
Last years I made myself familiar with Python and Haskell. I am surprised and impressed about the short and readable code you can write in these 2 languages, especially in comparison to languages like ...
1
vote
0
answers
88
views
How Functional Programming addresses concurrent increment/decrement operations invoked by different users? [duplicate]
Using Functional language, How can 2 different parties achieve the result of increment/decrement operations concurrently?
For the below scenario, Let's say, I've 2 quantities in stock and 2 users in ...
1
vote
2
answers
951
views
How Functional Programming addresses concurrent increment/decrement operations invoked by different users?
Using Functional language, How can 2 different parties achieve the result of increment/decrement operations concurrently?
For the below scenario,
Let's say, I've 2 quantities in stock and 2 users in ...
37
votes
7
answers
9k
views
What are the functional equivalents of imperative break statements and other loop checks?
Let's say, I've the below logic. How to write that in Functional Programming?
public int doSomeCalc(int[] array)
{
int answer = 0;
if(array!=null)
{
for(...
2
votes
3
answers
1k
views
Can functional programming used for solving problems which require randomness?
This older question tells us that in functional programming "true" randomness cannot be achieved since in FP functions are pure/idempotent and return the same value irrespective of number of ...
8
votes
1
answer
2k
views
What is the reasoning behind making non-determinism a feature of Haskell?
We know that in Prolog - non-deterministic predicates are a feature used to whittle down combinatorial problems.
In Haskell we see some similar non-deterministic behaviour to Prolog in the List ...
12
votes
1
answer
593
views
What are some intuitions that support calling the Maybe constructor in Haskell "Just"?
The intuition of an optional type like Maybe Int is that either there is no Int (thus, there's Nothing there) or that there is some Int; there is something there.
It makes sense to me that we call the ...
1
vote
2
answers
461
views
How is it obvious that that (foldr Cons Nil) just copies a list?
I am currently reading Why Functional Programming Matters by John Hughes.
In the "Gluing Functions Together" section, after having explained that (foldr f a) is a function that replaces all ...
9
votes
1
answer
899
views
Should I use functions based on Applicatives or Monads?
There are a lot of functions that accomplish the same thing, but using Applicative vs Monad definitions.
Some examples are:
(<*>) vs ap
pure vs return
(*>) vs (>>)
traverse vs mapM
...
1
vote
1
answer
471
views
How to automate Id generation?
I'm working on yesod application which has routes like
/make/something MakeR POST
on which server generates an object and returns its ID wrapped into JSON. I use Int as ID.
So, I ended up using a ...
1
vote
1
answer
149
views
Memoization of interdependent haskell functions
I have three functions which act on a matrix and kind of find a minimum sum path (Note dim = 80, See https://projecteuler.net/problem=82):
-- f is the minimum cost from x, y by taking only up and ...
0
votes
1
answer
233
views
Memoization in case of recursive interdefined functions in Haskell/Functional Programming?
I was reading Memoization with recursion which tells how for a recursively defined function fun we can do memoization by:
-- Memoization
memoize f = (map f [0 ..] !!)
-- Base cases
g f 0 = 0
g f 1 = ...
7
votes
3
answers
1k
views
How is arrow operator an Applicative Functor in Haskell?
Note: Still learning Haskell, not reached Monoids, studying Applicative Functors.
I saw this implementation and I am not entirely clear on it:
instance Applicative ((->) r) where
pure x = (\...
6
votes
3
answers
762
views
Better way of logging than manual calls to a logging function?
I'm writing a simple web server in Haskell, and putting in a bit of logging. I've got some calls to a logging function in a chain of IO >>=. It all feels a bit manual. Is there a better/more "...
8
votes
1
answer
331
views
IO Monadic code: standard vs flipped function composition
Below is a line that handles socket connections in a simple Haskell program.
mainLoop :: Socket -> IO ()
mainLoop sock = accept sock >>= forkIO . handle . fst >> mainLoop sock
The "...
4
votes
1
answer
493
views
What are some practical uses of Generalized Algebraic Datatypes? [closed]
All tutorials on GADTs that I've seen (in Haskell, Coq and Idris) use one same exapmle of a well-typed interpreter to show how GADTs can be useful, where you use the type index to encode the type of ...
3
votes
1
answer
865
views
CQRS/ES in haskell, using "Out of the tar pit" paper architecture
I find it difficult allying CQRS/ES with the "Out of tar pit" paper architecture.
This architecture implies 4 layers:
State (state of the application)
Business Domain (purely functional)
I/O
Control ...
2
votes
2
answers
304
views
how to model ADN & ARN in haskell?
I would like to modelize some nucleotid chains : ADN & ARN.
ADN is a list of nucleotids : A,T,G,C.
ARN is a list of nucleotids : A,U,G,C.
ideally, I would like to define e.g. ADN as a list of ...
11
votes
3
answers
2k
views
What is the difference between Haskell's type classes and Go's interfaces?
I am wondering if there is a difference between Haskell's type classes and Go's interfaces. Both define types based on functions, in that way, that a value matches a type, if a function required by ...
2
votes
2
answers
591
views
How does Haskell decide which memoized value will be discarded?
Haskell keeps the computed values of functions. This can be done only up the the storage limit. When the storage limit is reached, how does Haskell decide which computations to keep and which to ...
5
votes
1
answer
1k
views
Can variables be treated as constants in Haskell?
This is an embarrassingly basic question, but until recently I have avoided properly understanding IO in Haskell and now I need to.
I am writing (with someone else) a program that takes as part of ...
13
votes
1
answer
1k
views
Is it possible to prove a function is idempotent?
Is it possible to use static or dependent types to prove a function is idempotent?
I've searched Google and various places on StackOverflow/StackExchange for the answer with no luck. The closest I've ...
2
votes
2
answers
716
views
Concerns on lazy evaluation and infinite data structures
I am trying to learn how lazy evaluation works because I'm going to implement try to implement it in the programming language I'm developing (I know it isn't the best thing to do (to try to implement ...
2
votes
3
answers
2k
views
What is a Haskell value
What is the meaning of the term "value" in statements such as:
A Haskell function is a first-class value
Every value has a type
In both statements, I am left wondering what a value is and have a
...
1
vote
1
answer
284
views
Are Foldable and Traversable replacement for Uniplate?
I have a kind of minimal language (sort of parser for CISCO-like CLI) that have parser described below. The parser should be serializable to JSON, that's why not Parsec or Attoparsec.
I need a sort ...
3
votes
1
answer
1k
views
What is the difference between Applicative and Generative Modules, and Type Classes?
Reading the comments to this blog post made me realize I don't know much about some really interesting functional mechanisms between languages like Haskell, OCaml and Standard ML. I'd love a high-...
6
votes
2
answers
596
views
Movable object design in Haskell
Suppose I have an object which has a point denoting it's location and shape.
type Point = (Double, Double)
data Object =
Object { location :: Point
, shape :: Shape }
where a shape ...