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 = [] result false (assuming don't care negative n in strange ways)
  • if n = 0 , xs non-empty it's true
  • 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 using foldr 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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -