Skip to main content

All Questions

Tagged with
0 votes
3 answers
107 views

Solving Ambiguous type variable errors without adding a specific type annotation

I would like to make this code compile and run: import Data.List main = print $ elemIndices [] [] Without adding specific type annotations. The error it gives is: Ambiguous type variable ‘a0’ arising ...
Darren Smith's user avatar
1 vote
2 answers
113 views

Take elements of a list up to and including the first value that satisfies some predicate in Haskell

Given a predicate p, takeWhile p xs gives you the longest prefix of elements satisfying p. How can I get the shortest prefix containing an element that satisfies p?
Brendan Langfield's user avatar
1 vote
2 answers
103 views

How do I join a list of chars using a separator?

I am basically looking for the Haskell equivalent of the following python code: ' '.join(['_','a','b','1']) I understand that python treats those as strings instead of chars, but... I digress. MRE: [...
kesarling's user avatar
  • 2,280
0 votes
2 answers
86 views

How do I overload a certain operator in haskell to take different types on either side?

MRE: class Foo s where myCons :: Char -> s -> s myCons c xs = <my definition of however I wish to interpret this> instance (Eq, Show) Foo where (:) x y = x `myCons` y Error: ...
kesarling's user avatar
  • 2,280
4 votes
3 answers
183 views

Why is `tranpose [[]] == []` in Haskell?

Consider the following? ghci> transpose [] [] ghci> transpose [[]] [] ghci> transpose [[[]]] [[[]]] ghci> transpose [[[[]]]] [[[[]]]] ghci> transpose [[[[[]]]]] [[[[[]]]]] ghci> ...
Brendan Langfield's user avatar
0 votes
2 answers
78 views

How to create a list of elements of an indexed data type, whose length depends on the index

I am busy formalising a theorem using a library which introduces an indexed datatype. For simplicity one can think of it to have the form data idx (n : ℕ). Now I want to create a list of elements over ...
user11718766's user avatar
2 votes
2 answers
87 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 ...
Materia Gravis's user avatar
1 vote
2 answers
78 views

Haskell foldr1 lambda function which adds tuple values

I have been scratching my head trying to figure this out. How do I use foldr1 (or any other fold for that matter) in order to get the sum of tuples in a list. Example: list = [(1,2), (3,4)] sum = 10 ...
SniperWiper's user avatar
3 votes
3 answers
93 views

Haskell basic - pattern matching vs list tail

I'm doing Set7.hs from haskell.mooc.fi/ Exercises7 -- Ex 5: reverse a NonEmpty list. -- -- PS. The Data.List.NonEmpty type has been imported for you -- below doesn't work -- reverseNonEmpty :: ...
bastiat's user avatar
  • 2,071
1 vote
0 answers
71 views

Replacing single elements in list

So, I am currently doing an exercise where I have a list of employees, with the data type Employee. data Employee = Employee Name Age Salary deriving (Show) employees :: [Employee] employees = [ ...
Armin Haas's user avatar
1 vote
2 answers
110 views

understand haskell logic behind odd's and even's index lists

I had a question in haskell that asks a function to divide a list in two different lists so the even indexs filled a list and the odd ones another. Example: func :: [Int] -> ([Int], [Int]) Then, ...
Gabriel Monteiro Silva's user avatar
2 votes
1 answer
169 views

Is There an O(1) Way To Give Haskell Read Access to C-style Arrays?

I want to call a Haskell function from C++: a = f(x); where f is a Haskell function. x is double[] in cpp. Whatever stateful mutations happen to x will happen in C++. For example, x[42] = x[41] + pi; ...
James Strieter's user avatar
3 votes
3 answers
96 views

Define variable in list comprehension format function

I have this pseudocode that calculates the factorial of a number given by the user. Process without_title Define i, tmp, x As Integers; tmp<-1; Write "Enter a number to ...
Alejandro Caro's user avatar
0 votes
4 answers
150 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]] -- ...
coderodde's user avatar
  • 997
0 votes
0 answers
71 views

Joining NonEmpty types in Haskell

If I have two generic objects of type (NonEmpty a), how can I join those two objects into a new (NonEmpty a), just like ++ does with lists? I was expecting to be able to do something like the ...
Capybara's user avatar

15 30 50 per page
1
2 3 4 5
156