Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Lists where
- -- 4.1.1
- {--
- *Lists> :t sum
- sum :: (Num a, Foldable t) => t a -> a
- *Lists> :t map
- map :: (a -> b) -> [a] -> [b]
- *Lists> map (^2) [1,3,5]
- [1,9,25]
- *Lists> map (\x -> x*x) [1,3,5]
- [1,9,25]
- *Lists> let f = (^2) in map f [1,3,5]
- [1,9,25]
- *Lists> sqrt (sum [1,3,5])
- 3.0
- --}
- sumOfSquares x = sum (map (^2) x)
- {--
- *Lists> sumOfSquares [1,3,5]
- 35
- --}
- -- 4.1.2
- {--
- *Lists> :t (:)
- (:) :: a -> [a] -> [a]
- *Lists> :t (++)
- (++) :: [a] -> [a] -> [a]3
- *Lists> 3 : [5,7]
- [3,5,7]
- *Lists> [3,5] ++ [7]
- [3,5,7]3
- *Lists> 3:5:[7]
- [3,5,7]
- *Lists> 3:(5:[7])
- [3,5,7]
- --}
- f(x:xs)=(x,xs)
- {--
- *Lists> f[3,5,7]
- (3,[5,7])
- *Lists> f[3]
- (3,[])
- *Lists> f[]
- *** Exception: lab4.hs:42:1-14: Non-exhaustive patterns in function f
- --}
- sumOfSquares' [] = 0
- sumOfSquares' (x:xs) = x^2 + sumOfSquares' xs
- {--
- *Lists> sumOfSquares' []
- 0
- *Lists> sumOfSquares' [1,3,5]
- 35
- --}
- -- 4.2
- {--
- let f0(x:xs) = x
- *Lists> let f1(_:x:xs) = x
- *Lists> let f2(_:_:x:xs) = x
- *Lists> let f1(_:x:xs) = x
- *Lists> let f2(_:_:x:xs) = x
- *Lists> f1 [1,3,5,7,9]
- 3
- *Lists> f1[5,7]
- 7
- *Lists> f1[5]
- *** Exception: <interactive>:4:5-18: Non-exhaustive patterns in function f1
- *Lists> f1[3]
- -- *** Exception: <interactive>:4:5-18: Non-exhaustive patterns in function f1
- --}
- -- 4.2.1
- sum1 [ ] = 0
- sum1 [_] = 0
- sum1 (_:x:xs) = x + sum xs
- {--
- <interac
- *Lists> sum1 [ ]
- 0
- *Lists> sum1 [4]
- 0
- *Lists> sum1 [2,3,5,7,8]
- 23
- --}
- -- 4.3
- {--
- *Lists> :t "Ala"
- "Ala" :: [Char]
- *Lists> ['A','l','a']
- "Ala"
- *Lists> "Ala" ++ " ma kota"
- "Ala ma kota"
- *Lists> [3,5] ++ [7,9]
- [3,5,7,9]
- *Lists> let mala x = 'a' <= x && x <= 'z'
- *Lists> mala 'x'
- True
- *Lists> mala 'C'
- False
- --}
- countLower :: String -> Int
- countLower [] = 0
- countLower (x:xs) | 'a' <= x && x <= 'z' = 1 + countLower xs
- | otherwise = countLower xs
- {--
- *Lists> countLower "mala"
- 4
- *Lists> countLower []
- 0
- *Lists> countLower "qbcdefghi"
- 9
- *Lists>
- --}
- cgtx :: Ord a => a -> [a] -> Int --a nalezy do typow uporzadkowanych
- cgtx x [] = 0
- cgtx x(y:ys) | x < y = 1+ cgtx x ys
- | otherwise = cgtx x ys
- gtx :: Ord a => a -> [a] -> [a]
- gtx x [] = [ ]
- gtx x (y:ys) | x < y = y : gtx x ys
- | otherwise = gtx x ys
- {-- 4.5
- *Lists> cgtx 2 [5, 4, 3,1]
- 3
- *Lists> cgtx 5 [-1, -3, -4, 7, 8, 9]
- 3
- *Lists>
- --}
- {-- 4.6
- *Lists> read "52"
- *** Exception: Prelude.read: no parse
- *Lists> read "52" :: Int
- 52
- *Lists> show 52
- "52"
- --}
- string2int x = read x :: Int
- string2int' :: String -> Int
- string2int' x = read x
- {--
- *Lists> string2int "54"
- 54
- *Lists> string2int' "54"
- 54
- --}
- {-- 4.7
- [1..5]
- [n | n <- [1..5]]
- valueOf f x =
- f x
- {--
- *Lists> valueOf(^4) 2
- 16
- *Lists> valueOf(\x -> x^4) 2
- 16
- *Lists> let f = (^4) in valueOf f 2
- 16
- *Lists>
- --}
- suma1 = sum(map(1/)[1..100])
- suma1' = sum [1/n | n <- [1..100]]
- --}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement