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.

haskell
Filter by
Sorted by
Tagged with
0 votes
1 answer
30 views

Byte Order Problems When reading constants in Haskell

When I run the code: import Control.Applicative import Control.Concurrent import Control.Monad import Network.Socket import System.Environment import System.IO import Numeric (showHex) import ...
-1 votes
1 answer
18 views

GitHub Autograder Tasty Handle Writing Exception

I'm unit testing the following Haskell function in Tasty for GitHub's autograder: countLetters :: IO [Int] countLetters = do putStr "First entry: " x <- ...
-1 votes
1 answer
91 views

Bondage Discipline: =, <-, ->

These all mean the same (but the syntactic options seem a little quirky): let x = <expr> in ... let (x) = <expr> in ... let (((x))) = <expr> in ... -- any depth of ...
0 votes
0 answers
22 views

How do I compute sine, cosine, log function with arbitrary precision number from Data.Scientific library

I would like to use arbitrary precision number to perform some calculation validations. The scientific is what I need, however it's not possible to compute sine, cosine, logarithm. This can be ...
7 votes
3 answers
1k views

Practical applications of Rank 2 polymorphism?

I'm covering polymorphism and I'm trying to see the practical uses of such a feature. My basic understanding of Rank 2 is: type MyType = ∀ a. a -> a subFunction :: a -> a subFunction el = ...
1 vote
1 answer
39 views

how to parse unary expressions using parser combinators?

I'm trying to parse expressions using my own handwritten parser combintor library in Haskell. already I can parse binary expressions with the code below, that I found in dhall project, but I do not ...
6 votes
2 answers
710 views

How to correctly define your 'company' Prelude

I have decided to use my own Prelude for a larger project (containing some libraries and some executables). The Prelude doesn't export some partial functions and exports some common functions (i.e. ...
80 votes
10 answers
14k views

Can Haskell functions be proved/model-checked/verified with correctness properties?

Continuing on from ideas in: Are there any provable real-world languages? I don't know about you, but I'm sick of writing code that I can't guarantee. After asking the above question and getting a ...
1 vote
2 answers
52 views

Print a list of lists row wise in Haskell

I'm trying to print a list of lists in Haskell. It's something I've successfully done before for two sub lists but I can't remember all the steps and I haven't seen something for an arbitrary number ...
1 vote
1 answer
73 views

Is it possible to convert a parser made with YACC to a Haskell parser combinator?

I am looking at updating an old project that contained a YACC parser generator for parsing a specific file type. I want to improve the performance of this tool and was considering updating the parser. ...
2 votes
1 answer
44 views

Are fold and unfold required when recursive types appear in a language?

Are fold and unfold required when recursive types appear in a language? I'm reading "Types and Programming Languages" book by Benjamin C. Pierce. Section 4, “Recursive Types,” introduces ...
0 votes
1 answer
22 views

Implementing I/O Tasty-Hunit testing for Github autograder

I've been given the task to implement a test for an IO function for GitHub's autograder and Tasty. The function code is as follows: countLetters :: IO [Int] countLetters = do putStr &...
0 votes
0 answers
45 views

is there a way to get a value without side effects out of another case?

I have this code. I want to find a way to extract the type of arguments that were fed into the Function and and give them to insert function so I can use them later in type checking for function calls....
1 vote
1 answer
63 views

About the different requirements of different steps in a monad transformer stack pipeline

The laser-focused answer I accepted for a previous question of mine, was both mind-bending and revealing, at the point that I reopened my Real World Haskell (what a book!) and decided to go the extra ...
6 votes
1 answer
74 views

Type match error from just an unused qualified import

I'm trying to build an app based on gitlib and want to write a little additional function with this type signature: MonadGit r m => m [RefName]. It's the same as gitlib's own listReferences, so I'...
1 vote
1 answer
21 views

Haskell criterion with two inputs

I want to benchmark a function with two inputs using criterion. However, it seems the function for benchmarking from criterion can only take one input. I'm quite new to Haskell, and I'm sure there ...
1 vote
2 answers
64 views

Filtering a List based on State in Haskell

I've been looking at the implementations of ListT to find a good way to do something like this: func list = do set <- get -- Data.Set el <- list guard $ Set.notMember el set return el I ...
1 vote
1 answer
65 views

List to Lists in Haskell

I have a CSV file with 2 columns and each one with 32100 rows. I need to import this data and separate each column into a list of lists with a thousand values each list. E.g., let column2_data = [[1,2,...
5 votes
2 answers
962 views

Maybe and Either monads, short-circuiting, and performance

Functional Programming in C++, at page 214, with reference to an expected<T,E> monad which is the same as Haskell's Either, reads [...] as soon as any of the functions you're binding to returns ...
1 vote
1 answer
23 views

Adding dependency with cabal in gitlab-ci

I have a project I normally build with nix and cabal, however I also want to publish my documentation onto gitlab pages. It's impractical to run my nix on the gitlab CI, so I've just been using cabal. ...
33 votes
1 answer
7k views

How can I disable Haskell warning in small block?

I want to disable warning only some block of code. I searched Google but only find file scope or global scope disable method. Using cabal file or pragma {-# OPTIONS_GHC #-} Can I disable warning ...
13 votes
1 answer
412 views

About the choice of where to apply the monad parameter of a monad transformer

Take the MaybeT monad transformer: newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } I wouldn't have expected a different definition for it, because Maybe is just kind of a box with a (...
1 vote
1 answer
45 views

Haskell debug adapter not found

I have followed the instructions on this site: https://www.haskell.org/get-started/ to set up haskell in VS code. When I try to run the haskell project I get a VS Code error message: haskell-debug-...
4 votes
2 answers
129 views

Can I use StateT/MaybeT/forever to eliminate explicit recursion from this IO action?

I have a program like this, start :: [Q] -> R -> IO R start qs = fix $ \recurse r -> do q <- select qs (r', exit) <- askQ q r (if exit then return else recurse) r' that ...
0 votes
1 answer
99 views

Parsing List of Either Type

I'm writing a parser for logic formulae, and it's failing to parse a list of type Either. The parser works for all cases, except substitution of functions into predicates -- Parsing P(f(x:X)=a:A,b:B) (...
2 votes
4 answers
160 views

What is '(Floating a, Num (a -> a))' in Haskell?

In Haskell, I just know that :type ((+)(1)) ((+)(1)) :: Num a => a -> a ((+)(1) 2 3 But how about :type abs(sqrt) abs(sqrt) :: (Floating a, Num (a -> a)) => a -> a Actually, I try ...
6 votes
2 answers
246 views

Why can an invalid list expression such as 2:1 be assigned to a variable, but not printed?

In the ghci terminal, the following variable assignment is allowed: a = [2:x | x <- [1, 1, 1]] However, when a is printed, the following error occurs: - No instance for (Num [Integer]) arising ...
30 votes
11 answers
22k views

Haskell on Windows Setup

Unfortunately I don't have access to a *nix box at work or at home. The only way I can play with Haskell is on windows. Anyone here using Haskell on Windows? What's your setup?
1 vote
0 answers
28 views

Haskell: Rigid type binding [duplicate]

The following code works as expected: data Test a = Test { sample :: [a], underflow :: a, width :: a, bounds :: Int -> a } test :: Num a => [a] -> a -> a -> ...
3 votes
1 answer
178 views

Names with single quotes in Template Haskell

Normally, when using Template Haskell, names of bindings and data constructors are quoted by prefixing them with a single quote: showName, justName :: Name showName = 'show justName = 'Just ...
11 votes
3 answers
2k views

Multiple files use the same module name:

when I type stack run I get no error message but when I type stack ghci I get this error about multiple files use the same name , how I can solve it ? (base) wejden@wejdenaydi:~/wejden$ stack ghci ...
1 vote
1 answer
113 views

Haskell: Is there a simpler way to express the function (\f g x y -> f (g x) (g y))? Using Applicative ((->) r)?

combinator :: (b -> b -> c) -> (a -> b) -> a -> a -> c combinator f g x y = f (g x) (g y) Is there a simpler way to express this function? I find myself using it quite frequently....
134 votes
10 answers
237k views

How can I get nth element from a list?

How can I access a list by index in Haskell, analog to this C code? int a[] = { 34, 45, 56 }; return a[1];
2 votes
1 answer
46 views

Example of Massiv Multidimensional Array from a lazy ByteString?

I'm working with large data arrays in the sciences, encoded into binary FITS files. For simplicity, let's say I have 2GB of 32 bit Floats saved into a file, intended to be read as a 2d array, and that ...
1 vote
1 answer
48 views

How do I create a GHC package environment file that's equivalent to not having one?

When using GHC without a package environment file, you'll be able to use several libraries that it ships with, such as containers and text. When you do have a package environment, though, you can only ...
2 votes
1 answer
31 views

Do I have to keep the caller of DBus.Client.export alive for as long as I want the exported interface at the object path to work?

If not, what keeps alive the functions I export to implement the interface? I am implementing a notification server in Haskell, and at the moment I have something like this, startServer :: IORef ...
2 votes
2 answers
118 views

Performance increase using `let`?

Compare the following two snippets: expensiveFunction :: [Integer] -> [Integer -> Bool] expensiveFunction [] = [const False] expensiveFunction (x:xs) = let temp = expensiveFunction xs in [...
1 vote
0 answers
68 views

Is there any signature that can make "both" defined as "both f = bimap f f" work with heterogeneous pairs? [duplicate]

This works > bimap succ succ (1, 'a') (2, 'b') but this doesn't > both f = bimap f f > both succ (1, 'a') <interactive>:11:12: error: • No instance for (Num Char) arising from the ...
4 votes
1 answer
473 views

Haskell FFI local c header

I want to call a C single header library with FFI. Here's the Nuk.hs {-# LANGUAGE CPP, ForeignFunctionInterface #-} module Main where import Foreign import Foreign.C.Types foreign import ccall ...
34 votes
7 answers
70k views

How to get nth element from a 10-tuple in Haskell?

I have to get nth element in a Haskell tuple. And the tuples are like this : (3,5,"String1","String2","String3","String4","String5","String6","String7","String8","String9","String10"). Can you give ...
2 votes
1 answer
72 views

To keep state in a function of type a -> ReaderT r IO b, is my only option to put an IORef in the closure? Or can I somehow use StateT?

Say I have to implement a function f :: Foo -> ReaderT Bar IO Baz that I have to pass to a consumer (i.e. I'll call c f) where Foo/Bar/Baz are imposed the consumer of the function, and that the ...
4 votes
2 answers
213 views

Can I have a monitor in XMobar that keeps state form one invocation to the next?

tl;dr I guess my question could be distilled to a Y/N question: "Can the IO monad keep state only via I/O actions?" In other words, is my understanding correct that if I have to write an ...
6 votes
1 answer
75 views

How to check from the REPL that a given constraint is satisfied?

For the sake of exploring libraries with complex types and typeclass instances, I want to check from the REPL whether a typeclass constraint is satisfied or not (provided all the relevant dataypes and ...
1 vote
1 answer
90 views

How to define free monads and cofree comonads in Lean4?

in Haskell we can define both of these in this way: data Free (f :: Type -> Type) (a :: Type) = Pure a | Free (f (Free f a)) data Cofree (f :: Type -> Type) (a :: Type) = Cofree a (f (Cofree f a)...
1 vote
1 answer
79 views

Brick forms with dynamic fields

In Brick.Forms , the way to create a new form is that you have a Haskell data type with some pre defined fields which then become the corresponding input in the brick form. This module provides an ...
3 votes
2 answers
45 views

How to organize public and private libraries in a cabal project?

I have a project with several modules, 2 are for exporting a public library (1 each, say A and B; of the two, B a kind of the back-end, and is used by A, but I'm exporting both because I think one ...
4 votes
1 answer
104 views

What is the name of this prism-like optic, and is it useful for anything?

I'm currently working through tutorials for the Haskell lens package, so I can better understand the underlying mathematical foundations. And I was working through Prisms. type Prism s t a b = forall ...
0 votes
1 answer
42 views

How do I run a custom built xmobar on multiple screens

I'm currently running a system installed xmobar on both of my monitors by using the -x parameter. My xmonad is custom built with stack and spawns two separate xmobar processes like so: main :: IO () ...
2 votes
2 answers
2k views

Haskell: show and pretty-print instance

I started Intelligent System studies at a university and our first language is Haskell. I must admit that I am not really familiar with it so far. Part of this week's task is to develop an algebraic ...
0 votes
0 answers
32 views

GHC libraries with FFI on Windows have "foreign export" symbols missing

I have the following Haskell source, from which I would like to compile a static library that can be linked into a C program: {-# LANGUAGE ForeignFunctionInterface #-} module Lib where printHello :: ...

1
2 3 4 5
1026