diff options
[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 |
