Skip to content

Latest commit

 

History

History
74 lines (54 loc) · 1.26 KB

rozwiazania1.md

File metadata and controls

74 lines (54 loc) · 1.26 KB

Solution

let odd x = x `mod` 2 == 1

let even x = x `mod` 2 == 0

filter odd [1..10]
filter even [1..10]

Solution

let myfilter x = x `mod` 4 == 0 && x `mod` 3 /= 0

filter myfilter [1..20]

Solution

Prelude> foldr (:) [] [1..]
[1,2,3,4,5,6,7,8,9,10,...

Prelude> foldl (>:) [] [1..]
-- ...

Solution

-- sum
sum = foldl' (+) 0

-- product
product = foldl' (*) 1

-- length
length = foldl' (\acc _ -> acc + 1) 0

-- map
map f = foldr (\x acc -> f x : acc) []

map f = foldr ((:) . f) []

Solution

-- (++)
[] ++ ys = ys
xs ++ [] = xs
(x:xs) ++ ys = x:xs ++ xy

-- reverse
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]

-- take
take _ [] = []
take n (x:xs) | n <= 0    = []
              | otherwise = x : take (n-1) xs

-- drop ("as" pattern)
drop _ [] = []
drop n xxs@(_:xs) | n <= 0    = xxs
                  | otherwise = drop (n-1) xs