-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclass.hs
127 lines (96 loc) · 2.98 KB
/
class.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-} -- cover all cases!
{-# OPTIONS_GHC -fwarn-unused-matches #-} -- use all your pattern matches!
--{-# OPTIONS_GHC -fwarn-missing-signatures #-} -- write all your toplevel signatures!
{-# OPTIONS_GHC -fwarn-name-shadowing #-} -- use different names!
{-# OPTIONS_GHC -fwarn-incomplete-uni-patterns #-} -- no incomplete patterns in lambdas!
import Prelude hiding (
gcd, lcm, (.), length, maximum,
map, filter, foldr, id,
(<*>))
gcd :: Int -> Int -> Int
gcd a 0 = a
gcd a b = gcd b (a `mod` b)
lcm :: Int -> Int -> Int
lcm a b = (a * b) `div` gcd a b
--template <class T>
--T id (T x) { return x ;}
---- Функции от по-висок ред
id :: a -> a
id x = x
-- композиция на две функции
-- scheme: (define (compose f g)
-- (lambda (x) (f (g x))))
compose :: (a -> b) -> (c -> a) -> c -> b
--compose = \f -> \g -> \x -> f (g x)
compose f g x = f (g x)
--(.) = compose
-- друг вариант за дефиниция на (.)
(.) :: (a -> b) -> (c -> a) -> c -> b
f . g = \x -> f (g x)
-- неподвижна точка
-- scheme: (define (fixpoint? f x) (= x (f x)))
isFixpoint :: Eq a => (a -> a) -> a -> Bool
isFixpoint f x = x == f x
-- производна
-- scheme: (define (derive f dx)
-- (lambda (x)
-- (/ (- (f (+ x dx)) (f x)) dx)))
derive :: (Double -> Double) -> Double -> Double -> Double
derive f dx x = (f (x+dx) - f x) / dx
-- n-то прилагане на функция
-- scheme: (define (repeated f n)
-- (if (= n 0)
-- id
-- (compose f (repeated f (- n 1)))))
repeated :: Int -> (a -> a) -> a -> a
repeated 0 _ = id
repeated n f = f . (repeated (n-1) f)
--repeated 0 _ x = x
--repeated n f x = f (repeated (n-1) f x)
--
--repeated 0 _ = \x -> x
--repeated n f = \x -> f (repeated (n-1) f x)
sqr x = x*x
---- Кортежи
-- (1,2) :: (Int, Int)
-- (,) :: a -> b -> (a,b)
-- fst
-- snd
first :: (a,b,c) -> a
first (x,_,_) = x
second :: (a,b,c) -> b
second (_,y,_) = y
third :: (a,b,c) -> c
third (_,_,z) = z
-- (,,) :: a -> b -> (a,b,c)
-- type Point, Triangle, Vector
type Point = (Double, Double)
type Triangle = (Point, Point, Point)
type Vector = Point
-- <+> <*>
--(1,2) <+> (3,4) ---> (4,6)
(<+>) :: Vector -> Vector -> Vector
(x,y) <+> (z,t) = (x+z,y+t)
(<*>) :: Vector -> Vector -> Vector
(x,y) <*> (z,t) = (x * z, y * t)
---- Списъци
-- [] е списък
-- h:t е списък, ако t е списък
-- [1,2,3,4] == 1:(2:(3:(4:[]))) = 1:2:3:4:[]
-- [1,2,3,4] :: [Int]
-- [1,2,3,4] !! 2 -> 3
length :: [a] -> Int
length [] = 0
length (_:xs) = 1 + length xs
-- (define (map f l)
-- (if (null? l) l
-- (cons (f (car l)) (map f (cdr l))))
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
filter :: (a -> Bool) -> [a] -> [a]
filter _ [] = []
filter p (x:xs) = if p x then x:filter p xs
else filter p xs
foldr = undefined
---- Задачки