All Questions

Tagged with
Filter by
Sorted by
Tagged with
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!
Fopa Léon Constantin's user avatar
144 votes
1 answer
3k views

Handling List-types with Esqueleto

I have data types defined as: data ComitteeView = CommitteeView { committeeId :: CommitteeId , committeeMembers :: [Person] } ...
nomen's user avatar
  • 3,656
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 ...
fredoverflow's user avatar
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 ...
Frank's user avatar
  • 4,381
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 ...
TheIronKnuckle's user avatar
72 votes
3 answers
19k views

What is Haskell's Stream Fusion

What is Haskell's Stream Fusion and how do I use it?
Daniel O's user avatar
  • 4,618
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 ...
DarthVader's user avatar
  • 54.1k
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) =&...
muhmuhten's user avatar
  • 3,341
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]]]
user avatar
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 ...
徐保钰's user avatar
  • 793
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 ((((...
Craig Innes's user avatar
  • 1,573
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 : ...
tonlika's user avatar
  • 717
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):...
Jon W's user avatar
  • 15.7k
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 ...
Guy's user avatar
  • 3,363
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.
alexkelbo's user avatar
  • 724
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 ...
maclunian's user avatar
  • 7,983
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 ...
Evan Carroll's user avatar
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]...
Jakub M.'s user avatar
  • 33.2k
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 ...
Jonno_FTW's user avatar
  • 8,689
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 = ...
BradStevenson's user avatar
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 ...
Linus Arver's user avatar
  • 1,361
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 ...
MarcoS's user avatar
  • 13.5k
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?
1775's user avatar
  • 661
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: ...
TheOne's user avatar
  • 10.9k
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]
bogatyrjov's user avatar
  • 5,354
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 ...
andandandand's user avatar
  • 22.1k
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> ...
Christopher's user avatar
  • 1,415
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 ...
DarioP's user avatar
  • 5,417
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 ...
BM.'s user avatar
  • 1,691
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 ...
Mantas Vidutis's user avatar
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 ...
Alex's user avatar
  • 1,175
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 ...
mort's user avatar
  • 13.3k
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 [...
Evan Carroll's user avatar
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 ...
quant_dev's user avatar
  • 6,211
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 ...
Ben's user avatar
  • 1,571
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 ...
handles's user avatar
  • 7,767
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 ...
Clinton's user avatar
  • 22.8k
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 ...
Carlos's user avatar
  • 5,425
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 ...
jon_darkstar's user avatar
  • 16.6k
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), (...
Pratik Deoghare's user avatar
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',...
fredoverflow's user avatar
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 ...
Joachim Breitner's user avatar
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 ...
raichoo's user avatar
  • 2,557
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)]
unj2's user avatar
  • 52.8k
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 ...
Frerich Raabe's user avatar
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, ...
limp_chimp's user avatar
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, ...
Carlochess's user avatar
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 :: ...
Damian Powell's user avatar
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 ...
user192837465's user avatar
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] [...
Colin Woodbury's user avatar

1
2 3 4 5
47