Last active
June 25, 2020 17:00
-
-
Save cyclotimia/8bc13ab98be10866025b to your computer and use it in GitHub Desktop.
Stepic Haskell course - Chapter 3. Lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addTwoElements :: a -> a -> [a] -> [a] | |
addTwoElements x y z = x : y : z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
Напишите функцию groupElems которая группирует одинаковые элементы в списке (если они идут подряд) и возвращает список таких групп. | |
GHCi> groupElems [] | |
[] | |
GHCi> groupElems [1,2] | |
[[1],[2]] | |
GHCi> groupElems [1,2,2,2,4] | |
[[1],[2,2,2],[4]] | |
GHCi> groupElems [1,2,3,2,4] | |
[[1],[2],[3],[2],[4]] | |
Разрешается использовать только функции, доступные из библиотеки Prelude. | |
--} | |
groupElems :: Eq a => [a] -> [[a]] | |
groupElems [] = [] | |
groupElems (x:xs) = accum xs [x] [] | |
where | |
accum [] acc all = reverse $ acc:all | |
accum (x:xs) (z:acc) all | x == z = accum xs (z:z:acc) all | |
accum (x:xs) (z:acc) all | otherwise = accum xs [x] ((z:acc):all) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
Реализуйте функцию isPalindrome, которая определяет, является ли переданный ей список палиндромом. | |
GHCi> isPalindrome "saippuakivikauppias" | |
True | |
GHCi> isPalindrome [1] | |
True | |
GHCi> isPalindrome [1, 2] | |
False | |
--} | |
isPalindrome :: Eq a => [a] -> Bool | |
isPalindrome xs | reverse xs == xs = True | |
| otherwise = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
Реализуйте функцию nTimes, которая возвращает список, состоящий из повторяющихся значений ее первого аргумента. Количество повторов определяется значением второго аргумента этой функции. | |
GHCi> nTimes 42 3 | |
[42,42,42] | |
GHCi> nTimes 'z' 5 | |
"zzzzz" | |
--} | |
nTimes:: a -> Int -> [a] | |
nTimes x n = take n $ repeat x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
Сформируйте список целых чисел, содержащий только те элементы исходного списка, значение которых нечетно. | |
GHCi> oddsOnly [2,5,7,10,11,12] | |
[5,7,11] | |
Для анализа четности можно использовать функции odd и even стандартной библиотеки. | |
--} | |
oddsOnly :: Integral a => [a] -> [a] | |
oddsOnly = filter odd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
Составьте список сумм соответствующих элементов трех заданных списков. Длина результирующего списка должна быть равна длине самого длинного из заданных списков, при этом «закончившиеся» списки не должны давать вклада в суммы. | |
GHCi> sum3 [1,2,3] [4,5] [6] | |
[11,7,3] | |
--} | |
import Data.List | |
sum3 :: Num a => [a] -> [a] -> [a] -> [a] | |
sum3 a b c = calculate $ transpose [a,b,c] where | |
calculate as = reverse $ calc [] as where | |
calc a [] = a | |
calc a (x:xs) = calc (sum x:a) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment