haskelltouppertolowerhaskell-prelude

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


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

Solution

  • 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