Haskell Integer Odd Digits Checker

I seem to be stuck in a question and have no idea how to approach it or what I'm doing wrong with my current code.

I need to write a function called oddDigits that takes a single integer argument and returns a boolean result. It should return True if and only if the argument is a positive integer with an odd number of digits. If the argument is zero or negative, the function should terminate with an error message.

In addition, you cannot convert an argument to a string. You must use recursion. I have a feeling that each digit can be stored in the list recursively, and then the length of the list can determine the answer.

So far I have this:

oddDigits :: Integer -> Bool 

lst = [] 

oddDigits x 
    | (x < 0) || (x == 0) = error 
    | x `mod` 10 ++ lst ++ oddDigits(x `div` 10) 
    | length(lst) `mod` 2 /= 0 = True
    | otherwise = False 

, . Haskell . ?

+4
3

, , . , , - ...

XKCD Wikipedia printer

, ... . . (x < 0) || (x == 0) - < == (infix 4) , ||. , GHCi:

Prelude> :i ==
class Eq a where
  (==) :: a -> a -> Bool
  ...
    -- Defined in ‘GHC.Classes’
infix 4 ==
Prelude> :i ||
(||) :: Bool -> Bool -> Bool    -- Defined in ‘GHC.Classes’
infixr 2 ||

||, . ,

oddDigits x 
  | x <= 0  = error "bla bla"
  | ...

"" . , , , , typechecking .. ; " " ( ), , . :

oddDigits x 
 | x <= 0                      = error "blearg"
 | length (show x)`mod`2 /= 0  = True
 | otherwise                   = False 

. , - True, True ... :

oddDigits x 
 | x <= 0     = error "blearg"
 | otherwise  = length (show x)`mod`2 /= 0

, , .

, . . :

oddDigits 1 = True
oddDigits x 
 | x <= 0     = error "blearg"
 | otherwise  = not . oddDigits $ x`div`10
+4

, . , , . , . .

, digs, ​​ , :

oddDigits x | x <= 0 = error
oddDigits x = odd . length $ digs x
+3

leftaroundabout , , 2,3 23. .

oddDigits x 
  | x <= 0     = error "blearg"
  | x < 10     = True
  | otherwise  = not . oddDigits $ x`div`10

, . , , / . . , .

oddDigits :: Integer -> Bool
oddDigits x
   | x <= 0 = False
   | otherwise = oddDigits' True x

oddDigits' :: Bool -> Integer -> Bool
oddDigits' t x
   | x < 10 = t
   | otherwise = oddDigits' (not t) $ x `div` 10

oddDigits' , Bool. , Bool , . "", "" :

oddDigits x
   | x <= 0 = False
   | otherwise = odd . oddDigits'' 1  $  x

oddDigits'' :: Integer -> Integer -> Integer.

0

Source: https://habr.com/ru/post/1667930/


All Articles