haskellpattern-matchingparse-error

Finding the last element of a list given a condition


I'm trying to write a function that finds the last element of a list of Ints given that all the Ints are greater than 100. This is the code I have written so far:

isLeast100All :: [Int] -> [Int]
isLeast100All list = filter (>100) list

lastList :: [Integer] -> Integer
lastList list' = case list' of
  isLeast100All list
    | list == []   -> 0
    | list == [x]  -> x
    | list == [x:xs] -> lastList xs

which gives me the error: "Parse error in pattern: isLeast100All"

I feel like there's something simple that I'm missing here but I'm not sure what it is. I'm basing my lastList function off the following definition:

lastList :: [Integer] -> Integer
lastList x = case x of
  [] -> 0
  [x] -> x
  x:xs -> lastList xs

Solution

  • You need to fix your types and use filter instead of map for isLeast100All:

    isLeast100All :: [Integer] -> [Integer]
    isLeast100All = filter (> 100)
    
    lastList :: [Integer] -> Integer
    lastList list' = case isLeast100All list' of
        []   -> 0 
        xs  -> last xs
    

    Or just:

    lastList :: [Integer] -> Integer
    lastList l = last $ 0 : filter (> 100) l