longEnough n xs
: checks if a list has more than n
elements.
Examples:
longEnough 2 [1..5] == True
longEnough 3 [1,2,3] == False
longEnough 0 [] == False
longEnough 20 [1..] == True
longEnough n xs
: checks if a list has more than n
elements.
Examples:
longEnough 2 [1..5] == True
longEnough 3 [1,2,3] == False
longEnough 0 [] == False
longEnough 20 [1..] == True
I guess this is homework and you are still learning the basics so I'll start by giving you some hints using the recursion instead of foldr
(as @dfeuer proposed):
First start by noting some obvious cases:
xs = []
then the result is always False
(assuming you don't care about negative n
in some strange ways)n = 0
and xs
is non-empty then it's always True
n > 0
xs
has more than one element Maybe you have some recursive idea to break the last case down?
Here is a skeleton:
longEnough :: Int -> [a] -> Bool
longEnough _ [] = False
longEnough 0 _ = True
longEnough n (_:xs) = let n' = (n-1) in undefined
For those cases - if you look closely you'll see that I even added more hints on the solution.
n
and what should happen to those ... I did not herefoldr
is all about you should probably try to implement this using foldr
tooSeems there is no more feedback coming from the OP so I guess I can as well post the solution as I would start with:
longEnough :: Int -> [a] -> Bool
longEnough _ [] = False
longEnough 0 _ = True
longEnough n (_:xs) = longEnough (n-1) xs
(Not really much left to do...)
Here are the mentioned test-cases:
λ> longEnough 2 [1..5]
True
λ> longEnough 3 [1,2,3]
False
λ> longEnough 0 []
False
λ> longEnough 20 [1..]
True