tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
13
Task/Range-extraction/Haskell/range-extraction-1.hs
Normal file
13
Task/Range-extraction/Haskell/range-extraction-1.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Data.List (intercalate)
|
||||
|
||||
extractRange :: [Int] -> String
|
||||
extractRange = intercalate "," . f
|
||||
where f :: [Int] -> [String]
|
||||
f (x1 : x2 : x3 : xs) | x1 + 1 == x2 && x2 + 1 == x3
|
||||
= (show x1 ++ '-' : show xn) : f xs'
|
||||
where (xn, xs') = g (x3 + 1) xs
|
||||
g a (n : ns) | a == n = g (a + 1) ns
|
||||
| otherwise = (a - 1, n : ns)
|
||||
g a [] = (a - 1, [])
|
||||
f (x : xs) = show x : f xs
|
||||
f [] = []
|
||||
2
Task/Range-extraction/Haskell/range-extraction-2.hs
Normal file
2
Task/Range-extraction/Haskell/range-extraction-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> extractRange $ [0..2] ++ 4 : [6..8] ++ 11 : 12 : [14..25] ++ [27..33] ++ [35..39]
|
||||
"0-2,4,6-8,11,12,14-25,27-33,35-39"
|
||||
Loading…
Add table
Add a link
Reference in a new issue