1
votes

I'm doing an exercise where I need to make a function called, allLow that has the signature of allLow :: String -> String that takes a String and prints out all the capital letters, recursively. So it will run like,

> allLow "WhERe IS MY PEPPErONI PIZZa?"
"hera"

Here's what I have:

allLow :: String -> String
allLow "" = ""
allLow (c:cs) = if lowerCase c = True then (c ++ (allLow cs))

I wrote a function called, "lowerCase" (Char -> Bool) which returns true if the given char is lowercase. I am assuming that I can test lowerCase's validity at each "index" of the string and pump out all uppercase letters that way, but it's not working. The error message isn't helping me. It's saying there is a parse error at '=' but Idk what that means. It's not supposed to be == True.

2
if upperCase c == True. Why isn't it supposed to be ==? Also, what if c isn't uppercase? Where's the else condition?shree.pat18
@shree.pat18 I thought that you use '=' for boolean operations and '==' for arithmetic operations. My function "lowerCase" prints out "False" if function is uppercase.Jubl
Nope. == is always for equality anywhere AFAIK, as long as the type is an instance of Eq.shree.pat18
Style comment: don' t use == True, it's redundant. You can simply write if upperCase c then ... else .... Even in English, "if it is true that c is upper case, then .." is more distracting than the direct "if c is upper case, then ....".chi

2 Answers

5
votes

@shree.pat18 addressed the problems, but I feel the need to point out that this is a textbook case for using the filter function.... The Haskell idiomatic solution would be

allLow = filter isLower
4
votes

Quite a few things wrong here:

  1. if requires an else as well.
  2. == is for comparison, = for assignment.
  3. ++ works on 2 lists, what you are looking for is (:) which appends an element to a list.

The correct function should look something like this:

allCaps :: String -> String
allCaps "" = ""
allCaps (c:cs) = if upperCase c == True then c : allCaps cs else allCaps cs

BTW, Data.Char has an isUpper function, unless creating that function is part of your exercise.

Demo