Problem 22

99 Haskell Problems

Problem 22

Create a list containing all integers within a given range.

Example:

* (range 4 9)
(4 5 6 7 8 9)

Example in Haskell:

Prelude> range 4 9
[4,5,6,7,8,9]

Solution:

range :: Int -> Int -> [Int]
range start end
    | start > end = []
    | start == end = [end]
    | otherwise = start:(range (start+1) end)

Avoid exception by having a check return empty list if start is greater than end. However, the language provided range function as

range start end = [start..end]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s