From 7bdeaff608c7710f476fce9c650b4e07a9dc69d1 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 15 Oct 2016 12:25:56 +0200 Subject: [PATCH] Finished basic exercises --- Lecture 02/scratch/Scratch.hs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Lecture 02/scratch/Scratch.hs b/Lecture 02/scratch/Scratch.hs index ffaa789..5e30dc2 100644 --- a/Lecture 02/scratch/Scratch.hs +++ b/Lecture 02/scratch/Scratch.hs @@ -3,12 +3,15 @@ module Scratch where neg :: Integer -> Integer neg x = (-x) +sumTo :: Integer -> [Integer] -> Integer +sumTo n nums = sumToAcc 0 n nums + sumTo20 :: [Integer] -> Integer -sumTo20 nums = sumTo20Acc 0 nums +sumTo20 nums = sumToAcc 0 20 nums -sumTo20Acc :: Integer -> [Integer] -> Integer -sumTo20Acc acc [] = acc -sumTo20Acc acc (x:xs) - | acc >= 20 = acc - | otherwise = sumTo20Acc (acc + x) xs +sumToAcc :: Integer -> Integer -> [Integer] -> Integer +sumToAcc acc _ [] = acc +sumToAcc acc n (x:xs) + | acc >= n = acc + | otherwise = sumToAcc (acc + x) n xs