Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

2lab-1_fp

.hs
Скачиваний:
8
Добавлен:
19.01.2023
Размер:
4 Кб
Скачать
☆
import Char
import List
import Prelude

fib 1 = 1
fib 2 = 1
fib n = fib(n-1) + fib (n-2)

fact 1 = 1
fact n = n*fact(n-1)

foo1 n m l = fib(fact n + 2*max m l - min m l)
  
-- задание 2 пункт а
removing :: Char -> String -> String
removing _ [] = []
removing c (x:xs)
    | x == c = removing c xs
    | otherwise = x:removing c xs
	
-- задание 2 пункт б
multiplyy (x:xs) = multiply (x:xs) 1 0
multiply[]_ _ = 1
multiply (x:xs) n k -- n- счетчик индекса k - счетчик четных элементов
			|mod x 2 == 0 = n * multiply xs (n+1) (k+1)
			|k == 0 && lengthh (xs)==0 = -1
			|otherwise = multiply xs (n+1) k
	
lengthh :: [a] -> Integer
lengthh []=  0
lengthh (x:xs)  =  1 + lengthh xs

-- задание 3 пункт а
foo3_1[] = True
foo3_1(x:xs) = ((ord x <= ord 'T')||(ord x > ord 'Z')) &&(foo3_1 xs)

-- задание 3 пункт б
foo3_2[] = True
foo3_2(x:xs)  = ( x `notElem` "13579") && (foo3_2 xs)
--(( isDigit x )&&(mod (digitToInt x) 2 == 0)) && (foo3_2 xs)	

-- 3 заданиеееее
foo33 x = (foo3_1 x) && (foo3_2 x)



--задание 4 шифратор
foo4 [] = []    
foo4  (x:xs) 
            | isAlpha x = if  isLower x then chr (ord 'a' + (ord x - ord 'a' + 3) `mod` 26)  : foo4 xs --обработка букв
                          else  chr (ord 'A' + (ord x - ord 'A' + 3) `mod` 26) : foo4 xs                
                 
            | otherwise = x: foo4 xs      --если символ – не буква , просто переписываем его в результирующую строку и переходим к обработке хвоста  

--задание 4 дешифратор
foo44 [] = []    
foo44  (x:xs) 
            | isAlpha x = if  isLower x then chr (ord 'a' + (ord x - ord 'a' -3) `mod` 26)  : foo44 xs --обработка букв
                          else  chr (ord 'A' + (ord x - ord 'A' -3) `mod` 26) : foo44 xs                
                 
            | otherwise = x: foo44 xs      --если символ – не буква , просто переписываем его в результирующую строку и переходим к обработке хвоста  
			
--5 задание
foo5 a b c = ((union(intersect a b)c)\\(intersect(intersect a b)c))

--6 задание

--1 аргумент - 8ричная запись
fromEight[]=0
fromEight(x:xs)=toDig(x)*(8^(length xs)) + (fromEight xs)

toDig '0' = 0
toDig '1' = 1
toDig '2' = 2
toDig '3' = 3
toDig '4' = 4
toDig '5' = 5
toDig '6' = 6
toDig '7' = 7

--2 аргумент - двоичная запись
fromTwo[]=0
fromTwo(x:xs)=toDig(x)*(2^(length xs)) + (fromTwo xs)

--результат - в римской записи

rom1 n
	| n >=1000 = "M" ++ rom1 (n-1000)
    | n >=500  = "D" ++ rom1 (n-500)
    | n >=100  = "C" ++ rom1 (n-100)
    | n >=50   = "L" ++ rom1 (n-50)
    | n >=10   = "X" ++ rom1 (n-10)
	| n >=9   = "IX" ++ rom1 (n-9)
    | n >=5    = "V" ++ rom1 (n-5)
	| n >=4    = "IV" ++ rom1 (n-4)
    | n >=1    = "I" ++ rom1 (n-1)
    | True    = ""
	
-- основная функция 

foo6 a b = rom1(fromEight(a) - fromTwo(b))


--7 задание
fromDigits :: [Integer] -> Integer
fromDigits xs = aux xs 0
    where aux [] acc = acc
          aux (x:xs) acc  = aux xs ((acc * 10) + x)

foo7 s1 s2
	|(y2<y1) = error "Invalid years"
	|(m1>12) = error "Invalid months"
	|(m2>12) = error "Invalid months"
	|((list !! (m1-1))<d1) = error "Invalid day"
	|((list !! (m2-1))<d2) = error "Invalid day"
	|rez < 0 = error "Error!"
	|otherwise =  show(rez `div` 365) ++ " let, " ++ show((rez `mod` 365)`div`((sum $ (take 12 list)) `div` 12)) ++ " mesyacev, "++ show(rez - 365*(rez `div` 365) - (sum $ (take ((rez `mod` 365)`div`((sum $ (take 12 list)) `div` 12)) list))) ++ " dney"
	where 
		y1 = read(drop 6 s1)::Int
		m1 = read(take 2 (drop 3 s1))::Int
		d1 = read(take 2 s1)::Int 
		y2 = read(drop 6 s2)::Int
		m2 = read(take 2 (drop 3 s2))::Int
		d2 = read(take 2 s2)::Int 
		list =[31,28,31,30,31,30,31,31,30,31,30,31]
		rez = (((head(drop (m1-1) list)) - d1 + 1) + (sum $ (drop m1 list))) + (((y2-1)-y1)*((sum $ (take 12 list)))) + (d2 + (sum $ (take (m2-1) list)))
-- олина хуета

foo[] k = k
foo(x:xs) k
			|(x<5) = foo xs (k+1)
			|otherwise = foo xs k
Соседние файлы в предмете Функциональное программирование