All Questions
2,329
questions
156
votes
5
answers
107k
views
Is there any haskell function to concatenate list with separator?
Is there a function to concatenate elements of a list with a separator?
For example:
> foobar " " ["is","there","such","a","function","?"]
["is there such a function ?"]
Thanks for any reply!
144
votes
1
answer
3k
views
Handling List-types with Esqueleto
I have data types defined as:
data ComitteeView = CommitteeView { committeeId :: CommitteeId
, committeeMembers :: [Person]
}
...
107
votes
1
answer
11k
views
Why do we have map, fmap and liftM?
map :: (a -> b) -> [a] -> [b]
fmap :: Functor f => (a -> b) -> f a -> f b
liftM :: Monad m => (a -> b) -> m a -> m b
Why do we have three different functions that ...
73
votes
4
answers
9k
views
Understanding a recursively defined list (fibs in terms of zipWith)
I'm learning Haskell, and came across the following code:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
which I'm having a bit of trouble parsing, in terms of how it works. It's very neat, I ...
73
votes
5
answers
8k
views
Left and Right Folding over an Infinite list
I have issues with the following passage from Learn You A Haskell (Great book imo, not dissing it):
One big difference is that right
folds work on infinite lists, whereas left ones don't! To put ...
72
votes
3
answers
19k
views
What is Haskell's Stream Fusion
What is Haskell's Stream Fusion and how do I use it?
67
votes
5
answers
115k
views
Haskell (:) and (++) differences
I'm sorry for a question like this. I'm not too sure about the difference of the : and ++ operator in Haskell.
x:y:[] = [x,y]
also
[x] ++ [y] = [x,y]
as for the reverse function which arose ...
65
votes
8
answers
67k
views
unique elements in a haskell list
okay, this is probably going to be in the prelude, but: is there a standard library function for finding the unique elements in a list? my (re)implementation, for clarification, is:
has :: (Eq a) =&...
64
votes
7
answers
39k
views
Is there a function to flatten a nested list of elements?
How can I flatten a nested list like this:
[1, 2, 3, 4] == flatten [[[1,2],[3]],[[4]]]
62
votes
4
answers
13k
views
Why use null function instead of == [] to check for empty list in Haskell?
I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says:
null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this ...
52
votes
2
answers
4k
views
Why are difference lists more efficient than regular concatenation in Haskell?
I am currently working my way through the Learn you a Haskell book online, and have come to a chapter where the author is explaining that some list concatenations can be inefficient: For example
((((...
52
votes
2
answers
11k
views
What does this list permutations implementation in Haskell exactly do?
I am studying the code in the Data.List module and can't exactly wrap my head around this implementation of permutations:
permutations :: [a] -> [[a]]
permutations xs0 = xs0 : ...
51
votes
13
answers
32k
views
Does Haskell have List Slices (i.e. Python)?
Does Haskell have similar syntactic sugar to Python List Slices?
For instance in Python:
x = ['a','b','c','d']
x[1:3]
gives the characters from index 1 to index 2 included (or to index 3 excluded):...
47
votes
4
answers
9k
views
Getting a list of all possible data type values in Haskell
If I have a data type say:
data Color = Red | Yellow | Green
Is there a way I can turn this into a list of type [Color] getting all possible values? [Red, Yellow, Green]
Perhaps this is a complete ...
46
votes
6
answers
9k
views
How to tell if a list is infinite?
Is there a way to tell if a list in Haskell is infinite? The reason is that I don't want to apply functions such as length to infinite lists.
43
votes
9
answers
80k
views
Replace individual list elements in Haskell?
I have a list of elements and I wish to update them:
from this: ["Off","Off","Off","Off"]
to this: ["Off","Off","On","Off"]
As I am somewhat new to Haskell, I have been using (x:xs)!!y to extract ...
43
votes
7
answers
5k
views
In Haskell, why isn't there a TypeClass for things that can act like lists?
I'm reading Learn You a Haskell and I'm wondering why so many things are acting like a list, and nothing in the Prelude is using the native facility of type classes to set this up:
"The bytestring ...
39
votes
6
answers
7k
views
Using return vs. not using return in the list monad
I started my Grand Haskell Crusade (GHC :) ) and I am a bit confused with monads and IO functions. Could anyone explain simply what is the difference between those two functions?
f1 = do x <- [1,2]...
37
votes
5
answers
68k
views
Finding index of element in a list in Haskell?
I have a function in Haskell which finds the maximum value of an exponentiation from a list:
prob99 = maximum $ map (\xs -> (head xs)^(head (tail xs))) numbers
What I need to find is the location ...
36
votes
13
answers
97k
views
Removing duplicates from a list in Haskell without elem
I'm trying to define a function which will remove duplicates from a list. So far I have a working implementation:
rmdups :: Eq a => [a] -> [a]
rmdups [] = []
rmdups (x:xs) | x `elem` xs = ...
34
votes
23
answers
24k
views
How to get every Nth element of an infinite list in Haskell?
More specifically, how do I generate a new list of every Nth element from an existing infinite list?
E.g. if the list is [5, 3, 0, 1, 8, 0, 3, 4, 0, 93, 211, 0 ...] then getting every 3rd element ...
34
votes
9
answers
31k
views
efficiently checking that all the elements of a (big) list are the same
Problem
Let us suppose that we have a list xs (possibly a very big one), and we want to check that all its elements are the same.
I came up with various ideas:
Solution 0
checking that all ...
33
votes
4
answers
25k
views
Grouping a list into lists of n elements in Haskell
Is there an operation on lists in library that makes groups of n elements? For example: n=3
groupInto 3 [1,2,3,4,5,6,7,8,9] = [[1,2,3],[4,5,6],[7,8,9]]
If not, how do I do it?
33
votes
4
answers
15k
views
Efficient queue in Haskell
How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse.
i.e:
...
31
votes
6
answers
81k
views
Merging two lists in Haskell
Can't figure out how to merge two lists in the following way in Haskell:
INPUT: [1,2,3,4,5] [11,12,13,14]
OUTPUT: [1,11,2,12,3,13,4,14,5]
31
votes
7
answers
22k
views
Understanding this matrix transposition function in Haskell
This matrix transposition function works, but I'm trying to understand its step by step execurtion and I don't get it.
transpose:: [[a]]->[[a]]
transpose ([]:_) = []
transpose x = (map ...
30
votes
4
answers
11k
views
Decrementing ranges in Haskell
I am very new to Haskell. Could someone please explain why defining a list like this returns an null list
ghci> let myList = [10..1]
ghci> myList
[]
However this works correctly.
ghci> ...
30
votes
3
answers
2k
views
Non-trivial algorithm conversion from imperative to functional
To enforce my (weak) functional programming skills, I am studying The NURBS book by Piegl and Tiller converting all the algorithms to Haskell. It is a very nice and instructive process but I got stuck ...
28
votes
7
answers
115k
views
Learning Haskell: How to remove an item from a List in Haskell
Trying to learn Haskell. I am trying to write a simple function to remove a number from a list without using built-in function (delete...I think). For the sake of simplicity, let's assume that the ...
27
votes
5
answers
32k
views
Lazy List of Prime Numbers
How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily?
I am new to Haskell, and would like to learn about practical uses of the lazy evaluation ...
26
votes
1
answer
440
views
Binding `len = length xs` and then calculating `len` causes GHC to consume lots of RAM
I found a strange thing about GHCi and lists.
This command takes some time to execute and just returns the right answer.
ghci> length [1..10^8]
100000000
However, binding this to a variable and ...
25
votes
3
answers
31k
views
Haskell: check if two lists are equal
I want to check if two lists A and B are equal, i.e., a1 == b1, a2 == b2,...
I have a working solution:
all (\x->x) zipWith $ (==) A B
Another idea is to do it recursively: a:as, b:bs ; check if ...
25
votes
4
answers
26k
views
What is [] (list constructor) in Haskell?
I'm Having problems understanding functors, specifically what a concrete type is in LYAH. I believe this is because I don't understand what [] really is.
fmap :: (a -> b) -> f a -> f b
Is [...
25
votes
5
answers
2k
views
Can you recognize an infinite list in a Haskell program? [duplicate]
Possible Duplicate:
How to tell if a list is infinite?
In Haskell, you can define an infinite list, for example [1..]. Is there a built-in function in Haskell to recognize whether a list has ...
24
votes
5
answers
17k
views
All combinations of elements of two lists in Haskell
Given two lists, [a, b] and [c, d], I'd like to get the following result:
[(a,c), (a,d), (b,c), (b,d)]
How can I do this in Haskell? Is there a built-in function for this, or should I implement one ...
24
votes
6
answers
11k
views
Comparing lists in Haskell, or more specifically what is lexicographical order?
I'm just beginning this nice hashkell beginners tutorial:
http://learnyouahaskell.com
on this page on lists he explains that lists are compared in compared in lexicographical order, he gives this ...
24
votes
3
answers
7k
views
Haskell: Lists vs Streams
I've noticed streams seem to act a lot like lists, except with constant time append. Of course, adding constant time append to lists isn't too complicated, and DList does exactly that.
Lets assume ...
23
votes
2
answers
11k
views
Haskell range notation to generate list. Unexpected output
I came across an exercise in one of my lectures that left me confused on the output of [2, 2 .. 2]. Why when entering [2, 2 .. 2] it generates an "infinite" list with 2's.
The way i understood the ...
23
votes
2
answers
16k
views
Using list elements and indices together
I've always found it awkward to have a function or expression that requires use of the values, as well as indices, of a list (or array, applies just the same) in Haskell.
I wrote validQueens below ...
22
votes
9
answers
21k
views
How to zip multiple lists in Haskell?
In python zip function accepts arbitrary number of lists and zips them together.
>>> l1 = [1,2,3]
>>> l2 = [5,6,7]
>>> l3 = [7,4,8]
>>> zip(l1,l2,l3)
[(1, 5, 7), (...
22
votes
4
answers
16k
views
count occurrences of elements [duplicate]
Counting all elements in a list is a one-liner in Haskell:
count xs = toList (fromListWith (+) [(x, 1) | x <- xs])
Here is an example usage:
*Main> count "haskell scala"
[(' ',1),('a',3),('c',...
22
votes
7
answers
30k
views
How do I take the last n elements of a list
To obtain the last n elements of a list xs, I can use reverse (take n (reverse xs)), but that is not very good code (it keeps the complete list in memory before returning anything, and the result is ...
21
votes
2
answers
404
views
Relation between `DList` and `[]` with Codensity
I've been experimenting with Codensity lately which is supposed to relate DList with [] among other things. Anyway, I've never found code that states this relation. After some experiments I ended up ...
20
votes
4
answers
11k
views
How do you write the function 'pairs' in Haskell?
The pairs function needs to do something like this:
pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)]
20
votes
3
answers
2k
views
Why would using head/tail instead of pattern matching make evaluation terminate?
As an exercise, I'm trying to define a ruler value
ruler :: (Num a, Enum a) => [a]
which corresponds to the ruler function
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2...
where the n'th element of ...
19
votes
2
answers
2k
views
Internal representation of Haskell lists?
Haskell supports some basic operations for recursing through lists, like head, tail, init and last. I'm wondering, internally, how Haskell is representing its list data? If it's a singly-linked list, ...
19
votes
4
answers
3k
views
Removing syntactic sugar: List comprehension in Haskell
Can I unsugar list comprehension in this expression:
[(i,j) | i <- [1..4], j <- [i+1..4]]
This is the output:
[(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
How can I, with map, filter and so on, ...
19
votes
2
answers
12k
views
How to create infinitely repeating list in Haskell?
I'm a C# guy trying to teach myself Haskell from Erik Meijer's Channel 9 webcasts. I came across an interesting puzzle which involved skipping every 'n' elements of a list using zip and mod.
every :: ...
18
votes
2
answers
18k
views
Index of element in list in Haskell
How can I get the index of the element I am at in haskell when I am using map ?
For example I have this list l = "a+bc?|(de)*fg|h" and I want to know the exact index of the element I am at when I ...
18
votes
2
answers
50k
views
Syntax for list construction / concatenation
I've only been at Haskell for two days now, and was wondering what the difference between the two function definitions below are:
Prelude> let swap (x1:x2:xs) = x2:x1:xs
Prelude> swap [1..5]
[...