How to use toUpper and toLower in Haskell without importing module Data.Char?

358 views Asked by At

So I am trying to write my own functions without help from imports and i am struggling to have a function that works the same way.

Here is what I have.

toLower'' :: [Char]-> [Char]
toLower'' [] = []
toLower'' (x : xs)
  | x `elem` ['a' .. 'z'] = toEnum (fromEnum x + 32) : toLower'' xs
  | otherwise = x : toLower'' xs

toUpper'' :: [Char] -> [Char]
toUpper'' [] = []
toUpper'' (x : xs)
  | x `elem` ['a' .. 'z'] = toEnum (fromEnum x - 32) : toUpper'' xs
  | otherwise = x : toUpper'' xs
1

There are 1 answers

0
jf_ On BEST ANSWER

toLower'' is matching the lower-case characters instead of the upper case. (ToUpper'' works). Fixed:

toLower'' :: [Char]-> [Char]
toLower'' [] = []
toLower'' (x : xs)
  | x `elem` ['A' .. 'Z'] = toEnum (fromEnum x + 32) : toLower'' xs
  | otherwise = x : toLower'' xs