From cbcdd298a90e85053795a636694796ed197018e4 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 18 Dec 2022 14:35:07 +0200 Subject: [FP/HW3] Reworked first exercise to use tail recursion --- Year 2/Functional Programming/hw3.hs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'Year 2/Functional Programming') 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 -- cgit v1.3.1-26-gb76a