Ad
  • Custom User Avatar

    Just in case: it has changed slightly.

    module Counting.Preloaded where
    import Control.Arrow ((***))
    
    data Nat = Z | S Nat deriving (Show, Eq, Ord)
    
    nat :: a -> (Nat -> a) -> Nat -> a
    nat a _ Z = a
    nat _ aa (S n) = aa n
    
    iterNat :: (a -> a) -> Nat -> (a -> a)
    iterNat _ Z = id
    iterNat aa (S n) = aa . iterNat aa n
    
    natUnderflow :: String
    natUnderflow = "Nat is non-negative"
    
    instance Num Nat where
      (+) = iterNat S
      a * b = iterNat (b+) a Z
      a - Z = a
      Z - b = error natUnderflow
      S a - S b = a - b
      abs = id
      signum Z = Z
      signum (S _) = S Z
      fromInteger x
        | x < 0 = error natUnderflow
        | x == 0 = Z
        | otherwise = S $ fromInteger $ x - 1
    
    instance Enum Nat where
      toEnum x
        | x < 0 = error natUnderflow
        | x == 0 = Z
        | otherwise = S $ toEnum $ x - 1
      fromEnum x = iterNat (+1) x 0
    
    instance Real Nat where toRational = toRational . fromEnum
    
    instance Integral Nat where
      quotRem a Z = error "divide by zero"
      quotRem a b = until ((< b) . snd) (S *** subtract b) (Z, a)
      divMod = quotRem
      toInteger n = iterNat (+1) n 0
    
    
  • Custom User Avatar

    I have updated description ("Order of inhabitants in infinite lists won't be tested."), hope it clarifies it. It was complementary task, so I think strictness is unnecessary.

  • Custom User Avatar

    Thanks a lot!

  • Custom User Avatar

    Here:

    module Counting.Preloaded where
    import Control.Arrow ((***))
    
    data Nat = Z | S Nat deriving (Show, Eq, Ord)
    
    nat :: a -> (Nat -> a) -> Nat -> a
    nat a _ Z = a
    nat _ aa (S n) = aa n
    
    iterNat :: (a -> a) -> a -> Nat -> a
    iterNat _ a Z = a
    iterNat aa a (S n) = iterNat aa (aa a) n
    
    natUnderflow :: String
    natUnderflow = "Nat is non-negative"
    
    instance Num Nat where
      (+) = iterNat S
      a * b = iterNat (+a) Z b
      a - Z = a
      Z - b = error natUnderflow
      S a - S b = a - b
      abs = id
      signum Z = Z
      signum (S _) = S Z
      fromInteger x
        | x < 0 = error natUnderflow
        | x == 0 = Z
        | otherwise = S $ fromInteger $ x - 1
    
    instance Enum Nat where
      toEnum x
        | x < 0 = error natUnderflow
        | x == 0 = Z
        | otherwise = S $ toEnum $ x - 1
      fromEnum = iterNat (+1) 0
    
    instance Real Nat where toRational = toRational . fromEnum
    
    instance Integral Nat where
      quotRem a Z = error "divide by zero"
      quotRem a b = until ((< b) . snd) (S *** subtract b) (Z, a)
      divMod = quotRem
      toInteger = iterNat (+1) 0
    
  • Custom User Avatar

    Can you publish the Counting.Preloaded for local testing?

  • Custom User Avatar

    I managed the pass the shorter ones, but the ones with result being higher than 10^9 beats me in performance, it gets very slow (~45s).
    A hint for you: you don't have to track of the whole huge string, only the part you have to examine, and you have a limit for that.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution