CIS194/Lecture 02/scratch/Scratch.hs

18 lines
366 B
Haskell
Raw Normal View History

2016-09-24 21:09:51 +00:00
module Scratch where
neg :: Integer -> Integer
neg x = (-x)
2016-10-15 10:25:56 +00:00
sumTo :: Integer -> [Integer] -> Integer
sumTo n nums = sumToAcc 0 n nums
2016-09-24 21:09:51 +00:00
sumTo20 :: [Integer] -> Integer
2016-10-15 10:25:56 +00:00
sumTo20 nums = sumToAcc 0 20 nums
2016-09-24 21:09:51 +00:00
2016-10-15 10:25:56 +00:00
sumToAcc :: Integer -> Integer -> [Integer] -> Integer
sumToAcc acc _ [] = acc
sumToAcc acc n (x:xs)
| acc >= n = acc
| otherwise = sumToAcc (acc + x) n xs
2016-09-24 21:09:51 +00:00