-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP01_Lists.hs
49 lines (35 loc) · 1.66 KB
/
P01_Lists.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
{-# OPTIONS_GHC -Wall #-}
module P01_Lists where
-- Задача 1 -----------------------------------------
power3 :: [Integer]
power3 = [x ^ (3 :: Integer) | x <- [1..] :: [Integer]]
-- Задача 2 -----------------------------------------
toPower3 :: [Integer]
toPower3 = [(3 :: Integer) ^ x | x <- [1..] :: [Integer]]
-- Задача 3 -----------------------------------------
sumList :: [Integer] -> Integer
sumList xs = if null xs then 0 else head xs + sumList (tail xs)
sumPower3 :: Integer -> Integer
sumPower3 n = sumList (take (fromIntegral n) toPower3)
-- Задача 4 -----------------------------------------
sumPower :: Integer -> Integer -> Integer
sumPower m n = sumList (take (fromIntegral n) [m ^ x | x <- [1..] :: [Integer]])
-- Задача 5 -----------------------------------------
lessMe :: [Int] -> [Int]
lessMe xs = map (\x -> length (filter (\y -> y < x) xs )) xs
-- Задача 6 -----------------------------------------
hailstone :: Int -> Int
hailstone n = if mod n 2 == 0 then div n 2 else n * 3 + 1
-- Задача 7 -----------------------------------------
hailSeqHelper :: [Int] -> [Int]
hailSeqHelper xs = if head xs == 1 then xs else hailSeqHelper (hailstone (head xs) : xs)
hailSeq :: Int -> [Int]
hailSeq n = reverse (hailSeqHelper [n])
-- Задача 8 -----------------------------------------
allHailSeq :: [[Int]]
allHailSeq = [hailSeq x | x <- [1..]]
-- Задача 9 -----------------------------------------
find :: ([Int] -> Bool) -> [[Int]] -> [Int]
find p xs = if null xs then [] else if p (head xs) then head xs else find p (tail xs)
firstHailSeq :: Int -> Int
firstHailSeq n = head (find (\xs -> length xs == n) allHailSeq)