Comparisment

   - compare programming languages by example

36

List literals

The syntax for linked list literals.

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

oneTwoThree :: [Int]
oneTwoThree = [1,2,3]

Using square bracket syntax sugar.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

oneTwoThree :: Int
oneTwoThree = 1:2:3:[]

Using explicit cons and nil constructors as opposed to '[ ... ]' syntax sugar.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

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

Using range and step syntax.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

evensLessThanTen :: [Int]
evensLessThanTen = [n | n % 2 == 0, n <- [0..10]]

Using list comprehensions and range syntax.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

oneTwoThree = [1,2,3]

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

oneBTrue = [1,'b',True]

Lists may contain values of differing types.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

evensLessThanTen = [x for x in range(0,10) if x%2 == 0]

Using list comprehensions and ranges.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.