Andu Mîndrescu
Andu Mîndrescu

Reputation: 45

Why do I receive Non-exhaustive patterns in function compuneList

I am new to Haskell. I am trying to write a function called aplicaList which receives a number and list of functions [(a -> b)] , I want to return a list containing all the results [f(a)]

Function compuneList is a function which receives a function f and a list of other functions [(g)] and results a list of functions [f(g())].

compuneList :: (b -> c) -> [(a -> b)] -> [(a -> c)] 
compuneList f [] = []
compunelist f (ff : xs) = (f . ff : compuneList f xs)

aplicaList :: a -> [(a -> b)] -> [b]
aplicalist a [] = []
aplicaList a (f : xs) = (f(a) : aplicaList a xs)

The program compiles, but when I try to interpret aplicatList 9 [(+1), (+3)] I get:

[10,12 Exception: Lab4.hs:59:1-48: Non-exhaustive patterns in function aplicaList

and when I try aplicaList 9 (compuneList (+1) [sqrt, (^2), (/2)]) I get:

Exception: Lab4.hs:54:1-21: Non-exhaustive patterns in function compuneList

I do not know why in both cases. Thank you.

Upvotes: 0

Views: 75

Answers (1)

sshine
sshine

Reputation: 16105

The cause of your warnings are two typos:

  • aplicalist a [] = [] should be aplicaList a [] = [].
  • compunelist f (ff : xs) should be compuneList f (ff : xs).

Be aware of big and small letters when you code.


If you run your code through HLint, it will suggest that you can eliminate a bunch of parentheses.

You can also reduce explicit recursion using map:

compuneList :: (b -> c) -> [a -> b] -> [a -> c]
compuneList f = map (f .)

aplicaList :: a -> [a -> b] -> [b]
aplicaList a = map (\f -> f a)

Upvotes: 2

Related Questions