Main_Page/Snack Code/Functions as arguments

filter - use 'property' function and a list and return a new list constructed from elements of the old list that have a given property


filter':: (a -> Bool) -> [a] -> [a]
filter' _ [] = []
filter' p (x:xs) 
          | p x = x : filter' p xs
          | otherwise = filter' p xs

-- Example of property function
isEven :: Int -> Bool
isEven n = (n `mod` 2 == 0)

-- Example of using filter':
testFilter = filter' isEven [1,2,3,4]
-- returns [2,4]


(define (filter p ls)
  (cond ((null? ls) '())
        (else (let ((x (car ls)) (xs (cdr ls)))
                (if (p x) (cons x (filter p xs))
                    (filter p xs))))
(define (isEven n)
  (zero? (modulo n 2)))

(define testFilter (filter isEven '(1 2 3 4)))

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.