forked from elsys/fp-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listcomp.hs
31 lines (20 loc) · 857 Bytes
/
listcomp.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{-# OPTIONS_GHC -Wall #-}
module ListComp where
myLength :: [Int] -> Int
myLength list = sum [ 1 | _ <- list]
myElem :: Int -> [Int] -> Bool
myElem e list = not (null [x | x <- list, x == e])
myElem':: Int -> [Int] -> Bool
myElem' e l = and [True | x <- l, x == e]
myElem'':: Int -> [Int] -> Bool
myElem'' e l = or [ x == e | x <- l]
count :: Int -> [Int] -> Int
count e l = myLength [x | x <- l, x == e]
arithmeticSeries :: Int -> Int -> Int -> [Int]
arithmeticSeries a incrementator n = [a + x*incrementator | x <- [0..n-1]]
arithmeticSum :: Int -> Int -> Int -> Int
arithmeticSum a incrementator n = sum (arithmeticSeries a incrementator n)
geometricSeries :: Double -> Double -> Int -> [Double]
geometricSeries a q n = [a*q^x | x <- [0..n-1]]
geometricSum :: Double -> Double -> Int -> Double
geometricSum a q n = sum (geometricSeries a q n)