How to solve longEnough?. In Haskell -
longenough n xs
: checks if list has more n
elements.
examples:
longenough 2 [1..5] == true
longenough 3 [1,2,3] == false
longenough 0 [] == false
longenough 20 [1..] == true
i guess homework , still learning basics i'll start giving hints using recursion instead of foldr
(as @dfeuer proposed):
first start noting obvious cases:
- if
xs = []
resultfalse
(assuming don't care negativen
in strange ways) - if
n = 0
,xs
non-empty it'strue
- in other cases have
n > 0
xs
has more 1 element
maybe have recursive idea break last case down?
here skeleton:
longenough :: int -> [a] -> bool longenough _ [] = false longenough 0 _ = true longenough n (_:xs) = let n' = (n-1) in undefined
for cases - if look closely you'll see added more hints on solution.
ps
- maybe want think negative
n
, should happen ... did not here - if know
foldr
should try implement usingfoldr
too
solution
seems there no more feedback coming op guess can post solution start with:
longenough :: int -> [a] -> bool longenough _ [] = false longenough 0 _ = true longenough n (_:xs) = longenough (n-1) xs
(not left do...)
here mentioned test-cases:
λ> longenough 2 [1..5] true λ> longenough 3 [1,2,3] false λ> longenough 0 [] false λ> longenough 20 [1..] true
Comments
Post a Comment