write one compare function.
- first compare with string's length
- when string have same length, then compare string with lex order.
module CmpStr where
import Data.Function
import Data.Monoid
cmpStr :: String -> String -> Ordering
cmpStr = (compare `on` length) `mappend` compare
module CmpStr.Test where
import CmpStr
import Test.Hspec
import Test.QuickCheck
main = hspec $ do
describe "test for cmpstr" $ do
it "test cmpstr" $ do
cmpStr "abc" "defz" `shouldBe` LT
cmpStr "abcd" "def" `shouldBe` GT
cmpStr "abc" "abc" `shouldBe` EQ
cmpStr "abc" "def" `shouldBe` LT
Haskell is lazy, so we could get infinite sequence naturally.
like this.
module InfiniteSeq where
import Data.List
ones = repeat 1
nats = [0..]
merge = concat . transpose
merge2 x y = merge [x, y]
ints = 0: merge2 [1..] [(-1),(-2)..]
module Codewars.InfiniteSeq.Test where
import InfiniteSeq
import Test.Hspec
import Test.QuickCheck
import Test.Hspec.QuickCheck
main = hspec $ do
describe "test infinite" $ do
it "test ones" $ do
take 3 ones `shouldBe` [1,1,1]
it "test naturals" $ do
take 11 InfiniteSeq.nats `shouldBe` [0..10]
it "test ints" $ do
10 `elem` ints `shouldBe` True
(-10) `elem` ints `shouldBe` True