Comparisment

   - compare programming languages by example

53

List ranges

Create a list ranging between values and with some step.

Created by sam

Add a new answer

Either add an answer for a new language or add an alternative solution.
A description is only necessary when the code is not suitably 'obvious' or to justify an alternative approach.

Haskell

alphabet :: [Char]
alphabet = ['a'..'z']

Any instance of 'Enum' can be enumerated with list range syntax.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

positiveNumbers :: [Int]
positiveNumbers = [0..]

Lists can have an infinite range. Lazy evaluation means this list is only materialised as required. For example 'take 10 positiveNumbers' will return the first ten positive numbers as expected.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

evensBelowTen :: [Int]
evensBelowTen = [0,2..10]

A step quantity can be specified.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

fizzbuzzes :: [Int]
fizzbuzzes = [n | n <- [0..], (n `mod` 3 == 0), (n `mod` 5 == 0)]

List comprehensions can be used to filter out elements from a (possibly infinite) range.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

fizzbuzzes = [x for x in range(0,100) if x % 3 == 0 and x % 5 == 0]

List comprehensions can be used to filter out elements from a range.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

alphabet = [chr(x) for x in range(97,123)]

Functions can be applied to values obtained from a range.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.