Search code examples
haskellfilterbooleanlist-comprehension

misusage of the where in haskell


I am training in haskell and I would like to understand where I did go wrong.

The error is the infamous "variable not in scope"

The context is to determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once.

For example:

The quick brown fox jumps over the lazy dog.

Here is the code:

module Pangram (isPangram) where

isFalse n = n == False

transformText :: String -> [Bool]

transformText text =  [letter elem alphabet | letter <-text] where alphabet = "abcdefghijklmnopqrstuvwxyz"

isPangram :: String -> Bool
isPangram [] = False
isPangram text = case test of
         Just _ -> False
         Nothing -> True
            where test = find isFalse (transformText text)

Solution

  • the where should bind with the case part, so:

    isPangram :: String -> Bool
    isPangram [] = False
    isPangram text = case test of
             Just _ -> False
             Nothing -> True
        where test = find isFalse (transformText text)
    

    You can however simplify this. isFalse is equivalent to not :: Bool.

    Your program also does not test if every letter is used, it checks if every item in the string is a letter.

    You can replace the test with:

    isPangram :: String -> Bool
    isPangram text = all (`elem` text) ['a' .. 'z']