diff options
| Commit | cbcdd298a90e85053795a636694796ed197018e4 (patch) | |
|---|---|---|
| Author | 2022-12-18 14:35:07 +0200 | |
| Committer | 2022-12-18 14:35:07 +0200 | |
| Files | 02408459ccec634110851169193da5395ffce0b4 | |
| uni-projects-cbcdd298a90e85053795a636694796ed197018e4.tar uni-projects-cbcdd298a90e85053795a636694796ed197018e4.tar.gz uni-projects-cbcdd298a90e85053795a636694796ed197018e4.zip | ||
| Parent | 9ae3405e7b1593c7f52ddc37947a4fb6181ae29a (diff) | |
[FP/HW3] Reworked first exercise to use tail recursion
1 files changed, 8 insertions, 8 deletions
| -rw-r--r-- | Year 2/Functional Programming/hw3.hs | 16 |
diff --git a/Year 2/Functional Programming/hw3.hs b/Year 2/Functional Programming/hw3.hs index 2766fd9..6fc19b2 100644 --- a/Year 2/Functional Programming/hw3.hs +++ b/Year 2/Functional Programming/hw3.hs @@ -2,17 +2,17 @@ generate :: [Int] -> [Int] -> [[Int]] generate as bs = [x : take (takelen - skiplen) (drop skiplen alx) | x <- as, - let alx = tail (alterconif x as bs), + let alx = tail (alterconif x as bs []), takelen <- [1,3..(length alx)], skiplen <- 0 : [takelen - 1 | takelen > 1]] where - alterconif :: Int -> [Int] -> [Int] -> [Int] - alterconif cn a [] = [] - alterconif cn [] b = [] - alterconif cn a b - | (head a) < cn = alterconif cn (tail a) b - | (head b) < cn = alterconif cn a (tail b) - | otherwise = head a : head b : alterconif (head b) (tail a) (tail b) + alterconif :: Int -> [Int] -> [Int] -> [Int] -> [Int] + alterconif cn a [] accum = accum + alterconif cn [] b accum = accum + alterconif cn a b accum + | (head a) < cn = alterconif cn (tail a) b accum + | (head b) < cn = alterconif cn a (tail b) accum + | otherwise = alterconif (head b) (tail a) (tail b) (accum ++ [head a, head b]) -- Задача 2 |
