Slide 22
Slide 22 text
Recursividad
// JavaScript
const myFilter = (fn, arr) => {
const [head, ...tail] = arr;
return arr.length === 0 ? [] :
fn(head) ? [head].concat(myFilter(fn, tail)) :
myFilter(fn, tail)
}
// const greaterThan3 = x => x > 3
// const myArray = [1,2,3,4,5,6,7,3]
// const result = myFilter(greaterThan3, myArray)
// console.log('result', result)
-- Haskell
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter pred [] = []
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs
-- myFilter (\x -> x > 3) [1,2,3,4,5,6,7]
-- myFilter (>3) [1,2,3,4,5,6,7]