-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpract7.hs
67 lines (50 loc) · 1.31 KB
/
pract7.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
fact :: Integer -> Integer
fact 0 = 1
fact n = n * fact (n - 1)
f :: [Integer] -> Integer
f [] = 5
f (x:xs) = x
null' :: [a] -> Bool
null' [] = True
null' xs = False
head' :: [a] -> a
head' [] = error "Prazen spisak brat"
head' (x:xs) = x
tail' :: [a] -> [a]
tail' [] = error "no value here"
tail' (x:xs) = xs
take' :: Integer -> [a] -> [a]
take' 0 xs = []
take' n [] = []
take' n (x:xs) = x:(take' (n-1) xs)
drop' :: Integer -> [a] -> [a]
drop' 0 xs = xs
drop' n [] = []
drop' n (x:xs) = (drop' (n-1) xs)
(!) :: [a] -> Integer -> a
(!) xs n = head' (drop' n xs)
length' :: [a] -> Integer
length' [] = 0
length' (x:xs) = 1 + length' xs
magic = 1 : 1 : zipWith (+) (tail magic) magic
last' :: [a] -> a
last' (x:[]) = x
last' (x:xs) = last' xs
allButLast' :: [a] -> [a]
allButLast' (x:[]) = []
allButLast' (x:xs) = x:(allButLast' xs)
reverse' :: [a] -> [a]
reverse' [] = []
reverse' xs = last' xs : reverse' (allButLast' xs)
map' :: (a -> a) -> [a] -> [a]
map' f [] = []
map' f (x:xs) = (f x):(map' f xs)
apply' :: (a -> b) -> a -> b
apply' f a = (f a)
applyTwice :: (a -> a) -> a -> a
applyTwice f a = (f (f a))
filter' :: (a -> Bool) -> [a] -> [a]
filter' f [] = []
filter' f (x:xs)
| (f x) = x : (filter' f xs)
| otherwise =(filter' f xs)