Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Char
- -- 5.7.1
- -- > sum [x*x|x<-[1..100]]
- -- 338350
- -- 5.7.2
- replicate' :: Int -> a -> [a]
- replicate' x y = [y | _ <- [1..x]]
- -- 5.7.3
- pyths :: Int -> [(Int,Int,Int)]
- pyths n = [ (x,y,z) | x <- [1..n], y <- [1..n], z <- [1..n], (x * x + y * y == z * z) ]
- -- 5.7.4
- factors :: Int -> [Int]
- factors x = (\m -> nub(concat [ [x, y] | x <- [1..m], y <- [1..m], x * y == m ])) x
- perfects :: Int -> [Int]
- perfects x = [ n | n <- [1..x], sum ( filter (\z -> z /= n) ( factors n ) ) == n ]
- -- Got there. Not gracefully ;-)
- -- 5.7.5
- original :: [(Int,Int)]
- original = [ (x,y) | x <- [1,2,3], y <- [1,2,3] ]
- new :: [(Int,Int)]
- new = concat [ [ (x, y) | x <- [1..3]] | y <- [1..3] ]
- -- 5.7.6
- --
- positions :: Eq a => a -> [a] -> [Int]
- positions x xs = [ i | (x', i) <- zip xs [0..n], x == x' ]
- where n = length xs - 1
- find' :: Eq a => a -> [(a,b)] -> [b]
- find' x xs = [ i | (x', i) <- xs, x == x' ]
- positions' :: Eq a => a -> [a] -> [Int]
- positions' x xs = [ i | i <- find' x (zip xs [0..n])]
- where n = length xs - 1
- -- 5.7.7
- scaleproduct :: [Int] -> [Int] -> Int
- scaleproduct os es = sum [ a * b | (a, b) <- zip os es ]
- -- 5.7.8
- let2int :: Char -> Int
- let2int a = Char.ord a - Char.ord 'a'
- int2let :: Int -> Char
- int2let a = Char.chr (a + Char.ord 'a')
- shift :: Int -> Char -> Char
- shift n c | isLower c = int2let((let2int c + n) `mod` 26)
- | otherwise = c
- encode :: Int -> [Char] -> [Char]
- encode d xs = [ shift d r | r <- xs ]
- let2int' :: Char -> Int
- let2int a | isLower a = Char.ord a - Char.ord 'a'
- | isUpper a = Char.ord a - Char.ord 'A'
- int2let' :: Int -> Char
- int2let' a | IsLower = Char.chr (a + Char.ord 'A')
- shift' :: Int -> Char -> Char
- shift' n c | islower c = int2let((let2int c + n) `mod` 26)
- | isupper c = int2let((let2int c + n) `mod` 26)
- | otherwise = c
- encode' :: Int -> [Char] -> [Char]
- encode' d xs = [ shift d r | r <- xs ]
Add Comment
Please, Sign In to add comment