Questions tagged [haskell]
Haskell is a purely functional programming language featuring strong static typing, lazy evaluation, extensive parallelism and concurrency support, and unique abstraction capabilities.
50,897
questions
2
votes
1
answer
30
views
Reader Monad in Haskell. Where is the reader passed as argument?
On this monad reader example:
import Control.Monad.Reader
tom :: Reader String String
tom = do
env <- ask -- gives you the environment which in this case is a String
return (env ++ " ...
0
votes
1
answer
12
views
Why standalone deriving fails with rigid type on a record with existential quantification?
I am trying to compare associated types with existential ones, and wrote following snippet:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE StandaloneDeriving ...
1
vote
1
answer
64
views
Function isNumeric with error when calling it from other functions
I have this function taken from other user from stackoverflow site that checks if a string is a number.
import Data.Char
isNumericString :: String -> Bool
isNumericString "" = False
...
-2
votes
1
answer
63
views
In Haskell how should I ignore if a number is Int or Integer?
In my homework I have to write a program which tells me if 2 numbers can be devided, BUT when there is the input sometimes the number are Ints and sometimes Integers.
I tried it with all a. Num => ...
-4
votes
0
answers
55
views
Storing information that is in haskell and uses codeworld [closed]
for my assignment I need to figure out how to store the location of a bot on a cartesian plan. I have no idea how to do that. The bot has a pre list of commands imported from codeworld
Forward ...
1
vote
1
answer
52
views
Why this TCP echo code does not return anything?
I wrote this code:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString as BS
import Data.Text (Text)
import Data.Text.Encoding
import Data.Word
import Foreign
...
0
votes
0
answers
62
views
Printing free state monad as Haskell code
I've read articles about extending simple-reflect to functions and about representing the State monad as a free monad (here), so now I'd like to try to print some state monads as executable Haskell ...
0
votes
2
answers
106
views
Haskell's reducing rule
How does lengthGT4 $ produce id 1 reduced to True in Haskell?
lengthGT4 :: List a -> Bool
lengthGT4 (_ :. _ :. _ :. _ :. _ :. _) = True
lengthGT4 (_ :. _) = False -- deliberately doing this
...
1
vote
2
answers
73
views
In Haskell, how to putStrLn works in foldLeft
TL;DR.
foldLeft (\_ x -> putStrLn x) (pure ()) ("2":."1":.Nil)
Why the above code outputs
1
rather than
2
1
Full question
I am doing exercise of fp-course
https://github.com/...
1
vote
2
answers
79
views
Functor over multiple levels
I have this lame attempt:
fmap2 :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
fmap2 f f2 = (fmap2 f . fmap f2)
It is supposed to work like this:
fmap2 negate [[1,2], [3]] -- ...
0
votes
1
answer
30
views
Haskell dot operator with multiple parameters [duplicate]
I am doing exercise in fp-course
https://github.com/system-f/fp-course/blob/5e0848b4eacb3ddff65553ab5beb6447b73f61b3/src/Course/Applicative.hs#L357
The correct answer is
replicateA n ka = sequence $ ...
0
votes
1
answer
87
views
Haskell's "where" usage
I am doing an exercise of fp-course:
https://github.com/system-f/fp-course/blob/5e0848b4eacb3ddff65553ab5beb6447b73f61b3/src/Course/Applicative.hs#L77
Correct answer is
(<*>) fs as = foldRight ...
3
votes
1
answer
117
views
How Can One Express the Fact that `()` is a Subset of All Other (Non-`Void`) Types in Haskell?
I recently started learning Haskell, and I wrote the following code as part of a small parsing library:
-- Successful iff the input string has a length of zero
parseEOF :: Parser ()
parseEOF = Parser ...
4
votes
2
answers
89
views
Deleting an element of a set
im trying to delete an element from a set but I can't seem to get the syntax right. The datatype is defined as follows:
data Menge el = Menge [el] deriving (Eq, Ord, Show)
and the function is:
...
-1
votes
0
answers
51
views
How to find the type of "double" [duplicate]
My homework required me to find the type of double through using ":type" but I failed, can anyone tell me how to achieve it? Thank you!
the entire codes showed:
double x = x + x
double 21
...
0
votes
2
answers
79
views
Swap values of two IORefs
I need this functionality:
x <- newIORef "x"
y <- newIORef "y"
swapIORefs x y
readIORef x -- Outputs "y"
readIORef y -- Outputs "x"
My best attempt is:
...
2
votes
1
answer
55
views
Make Haskell type system understand that fundeps carries over a composite (tuple-like) type
I have the following code, which I use to convert a HList-of-touples (or more like, tree-of -touples) into the same shape, but instead of a tuple, just keeping the second element.
The problem is, this ...
1
vote
1
answer
68
views
How do I get an error and warning count when I run GHC?
If I run dotnet build to build a .NET project, at the end of the output, I see a message that tells me how many warnings and errors the compiler generated while building my project. I have attached an ...
0
votes
1
answer
64
views
Haskell, HIP library, decode function
Please help me understand how to use the decode function from the HIP library.
class ImageFormat format => Readable img format where
decode :: format -> B.ByteString -> Either String img
I ...
0
votes
1
answer
83
views
Haskell interpreter, dont know how to run and test it
I am working on an interpreter as a project task, in order to understand what I have to do, I decided to download the interpreter from the Internet and run it. I started the specified folder in ...
9
votes
0
answers
138
views
How unsafe is coercing typeclass dictionaries?
I've got situation where I had a product type (say T), and an instance of say C derived using anyclass deriving, which works because each member of T is an instance of C.
I changed one member of T to ...
1
vote
1
answer
61
views
"Ambiguous type variable" when using scoped variables in Haskell?
I got this OCaml code:
for x = 0 to 12 do
let i = (1 + (x * 3)) in
let j = (60 - (x * 5)) in
Printf.printf "I=%d J=%d\n" (i) (j);
done;;
And I translated it to Haskell, but it ...
1
vote
1
answer
46
views
Trigger completion within GHCi when using :main
I'm using the optparse-applicative library, which supports Bash-completion. I'd like to trigger such completion when typing :main <tab> within GHCi. Is there a way to instruct GHCi to use the ...
1
vote
1
answer
30
views
Haskell gnuplot not working displaying plot
I've been using the Glasgow Haskell Compiler (GHC) to learn Haskell. I installed the gnuplot package using Cabal (I used the command cabal install --lib gnuplot). I can load the gnuplot module, ...
0
votes
1
answer
39
views
Github actions fail for Mac with my Haskell package including C++ --- how to enable C++11 for Mac?
I have some Github actions for my library 'numerical-integration'. They all work fine except those on Mac. Among the warnings and error messages for Mac, there are:
note: expanded from macro '...
4
votes
1
answer
103
views
FFI returns a huge integer value instead of 0
I've wrapped the 'NumericalIntegration' C++ library in Haskell. Here is the latest version of the package (the version on Hackage is older).
Here is the main part of the C++ code :
class Integrand {
...
0
votes
1
answer
68
views
How can I make this hash implementation more parallel?
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
compress2 ::
UArray (Word8,Word8) Word8 ->
UArray Int Word8 ->
UArray Int Word8 ->
Int ->
...
1
vote
1
answer
41
views
The same function works when using GHCI, but not in VS code
I am new to Haskell, and I am trying to write a function that replaces the first and the last element of a an array. When writing my code in GHCI, it works fine, but in VS Code it gives an error ...
2
votes
0
answers
67
views
how to combine error with printf in haskell [duplicate]
I found the Haskell module Text.Printf.
It provides a sort of C-like printf. For example printf "hi %d\n" 42 will produce the string "hi 42\n".
That is pretty neat. Now something ...
0
votes
0
answers
43
views
How to change llvm version for m1 Mac to build Haskell project
I am trying to build a Haskell project but I keep getting this error:
You are using an unsupported version of LLVM!
Currently only 9 to 13 is supported. System LLVM version: 15.0.7
We will try ...
1
vote
1
answer
65
views
When returning a list pattern from a Haskell function how much of the original list is referenced?
This is a question regarding Haskell list pattern returns.
If I return a cons-based list pattern that matches one of the function inputs, does the function return a reference to the head of the ...
10
votes
2
answers
368
views
Parametrised classes
I have a class as follows:
class Token a where
symbol :: a -> String
I also want all instances of Token to have a function convert which returns a parametrised type. The conversion alone works ...
3
votes
1
answer
72
views
Is there any possible benefit to SPECIALIZE a free type variable?
I think parametricity implies it can never help to specialize a free type variable (i.e. one with no class constraints), like a function :: Maybe a -> Bool. Are there any cases in the context of ...
3
votes
1
answer
120
views
Can we always use <$> in Haskell to define functions "point free"?
I have been learning just how powerful the <$> and <*> operators are in Haskell, and how it is possible to define some functions without parameters where they would normally be needed. I ...
1
vote
1
answer
42
views
possible regression? type error compiling using ghc 9.6.2, hmatrix 0.20.2, and Vector 0.13.0.0
Compiling the following code using ghc 9.6.2, hmatrix 0.20.2, and Vector 0.13.0.0
import Numeric.LinearAlgebra
import qualified Data.Vector.Storable as V
nnz :: Vector R -> Int
nnz v = size $ V....
1
vote
1
answer
62
views
Haskell: Similar data constructors and boiler plate
I have a type constructor Token and all of its data constructor take an Int (the position where this token was encountered in the input) and maybe some payload:
data Token = EmptyToken Int -- just ...
0
votes
1
answer
87
views
Function that takes a variable (of any type) and determines if said variable contains a number
From this same site I extracted this function that checks if a string is a number.
isNumeric :: String -> Bool
isNumeric "" = False
isNumeric "." = False
isNumeric xs =
case ...
1
vote
2
answers
113
views
Assignment in Haskell within for block
I'm trying to make a function that calculates the factorial of a number. It is marking an error in the assignment and I don't know how to correct it.
for list action = mapM_ action list
main :: IO ...
1
vote
0
answers
114
views
Haskell data vs class
I've written an LALR1 parser which seems to work. It can be parameterised for the type of symbols it receives. The only binding is Eq.
module LALR.Parser where
data Rule symbolT = Rule
{
...
0
votes
1
answer
78
views
Deal cards in Haskell
I need to define a function
deal :: [String] -> [String] -> [(String,String)]
It needs to work like this:
deal ["Hercule","Ariadne"] ["Ace","Joker","...
4
votes
1
answer
80
views
How to shrink an union type in Haskell?
I'm using OpenUnion a type from world-peace library and I'd like to write a function which will transform an union into a narrower one if possible:
unionShrink :: Contains bs as => OpenUnion as -&...
2
votes
1
answer
192
views
Why is the assembly for this Haskell program fairly big w.r.t. equivalent C code? Or what am I doing wrong when compiling?
Once I asked Why is Haskell binary for simple "times 2" program so big (wrt e.g. C++)?, but that was duped to Small Haskell program compiled with GHC into huge binary.
I've decided to ask a ...
2
votes
1
answer
86
views
Application of Monads
The code below provides for testing the satisfiability of a 2-SAT boolean formula (a formula containing just clauses of the form X v Y). Although other algorithms exist (strongly connected components ...
1
vote
0
answers
50
views
Can nested monad be a monad? [duplicate]
I'm trying to make two nested monad to a monad, but can not find the solution.
I can make it Functor and Applicative, but can not find a way to make it a monad
newtype Man m n a = Man { runMan :: m (n ...
0
votes
1
answer
83
views
Can we use sum types in haskell FFI?
https://wiki.haskell.org/Foreign_Function_Interface#Data_structures
The document above only describes the case that only one data constructor is involved.
I'm looking for a method to define sum types ...
1
vote
3
answers
94
views
Expressing multi-parameter typeclasses in terms of single-parameter ones
For a certain reason (weird Hugs installation on target computer) I want Haskell98 compliant code, which means no common language extensions. One annoyance in particular is the absence of multi-...
2
votes
1
answer
48
views
Caching an expensive to compute result in a class instance
Lets say I've got something like the following
class C a where
f :: a -> Text
newtype T a = T a
expensiveFunction :: (Bounded a, Enum a, ToText a) => Map a Text
expensiveFunction = _
...
1
vote
3
answers
129
views
Define a default return value to Haskell function
I'm learning Haskell and I'm doing the H99 (99 Haskell Problems). I'm working on the first set of problems and the topic is lists. The exercise I'm working on asks to return the last element of a list....
1
vote
4
answers
74
views
How to unambiguously trim a string with parser combinators?
My current implementation with Text.ParserCombinators.ReadP:
trim :: ReadP String
trim = do skipSpaces
content <- some get
skipSpaces
eof
return content
The ...
3
votes
2
answers
112
views
Any difference between `Reader a (b -> c)` and `b -> Reader a c`?
(Some other mtl monads would bring up the same problem, but let's just use Reader here to exemplify)
Say I need to show something in a human-readable way, with the implementation and behaviour details ...