Questions tagged [range-notation]

Range notation is a shorthand used in Haskell for specifying a list containing a sequence of integers. For example, [1..10] means the set of integers [1,2,3,4,5,6,7,8,9,10]

Code             Result
----             ------
[1..10]          [1,2,3,4,5,6,7,8,9,10]
[2,4..10]        [2,4,6,8,10]
[5,4..1]         [5,4,3,2,1]
[1,3..10]        [1,3,5,7,9]
4 questions
47
votes
2 answers

Haskell ranges and floats

Why is the behavior of the Haskell range notation different for floats than for integers and chars? Prelude> [1, 3 .. 10] :: [Int] [1,3,5,7,9] Prelude> [1, 3 .. 10] :: [Float] [1.0,3.0,5.0,7.0,9.0,11.0] Prelude> ['a', 'c' .. 'f'] "ace" I would…
undur_gongor
  • 15,657
  • 5
  • 63
  • 75
23
votes
2 answers

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 notation was that the first element is the start…
Carlos
  • 5,405
  • 21
  • 68
  • 114
8
votes
2 answers

Haskell: Unexpected output for expression [0, 0.1 .. 1]

When evaluating the expression: *main> [0, 0.1 .. 1] I was actually expecting: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] But I was quite shocked to see the output…
Carlos
  • 5,405
  • 21
  • 68
  • 114
0
votes
3 answers

How and why is [1 .. 0] different from [1 .. -1] in Haskell?

I have defined the following function let repl x n = [x | _ <- [1..n]] which imitates the built-in replicate function. While experimenting with it, I noticed a strange thing: repl 10 0 evaluates to [], while repl 10 -1 produces an error: No…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434