Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. Zad 1.
  2. ----------------------------------
  3. msum x = sum $ map sum (map (\j -> map (\i -> if i/=j then (x!!j!!i)-i+j else (-1)^(x!!j!!i)) [0..(length x)-1]) [0..(length (x!!0))-1])
  4.  
  5. Zad 2.
  6. ----------------------------------
  7.  
  8. type Vector = [Float]
  9.  
  10. norm :: Vector -> Float
  11. norm v = sqrt $ sum $ map (^2) v
  12.  
  13. psub :: Num a => (a, a) -> a
  14. psub (a, b) = a - b
  15.  
  16. vsub :: Vector -> Vector -> Vector
  17. vsub v1 v2 = map psub $ zip v1 v2
  18.  
  19. perm :: Vector -> Vector
  20. perm v = (tail v) ++ [head v]
  21.  
  22. infperm :: Vector -> [Vector]
  23. infperm v = [perm v] ++ (infperm $ perm v)
  24.  
  25. perms :: Vector -> [Vector]
  26. perms x = take ((length x) - 1) $ infperm x
  27.  
  28. test :: Vector -> Int
  29. test x = sum [1 | u <- perms x, (norm (vsub x u )) > 0.6 * (norm x)]
  30.  
  31.  
  32. Zad 3.
  33. ----------------------------------
  34. data Frac = Num Int Int | NaN
  35. deriving (Eq)
  36.  
  37. instance Show Frac where
  38. show NaN = "NaN"
  39. show (Num _ 0) = show NaN
  40.  
  41. show (Num a b) | b == 1 = show a
  42. | a == b = show 1
  43. | otherwise = show a ++ "/" ++ show b
  44.  
  45. (.+) :: Frac -> Frac -> Frac
  46. (Num a b) .+ (Num c d) = Num (a*d + c*b) (b*d)
  47.  
  48. (.-) :: Frac -> Frac -> Frac
  49. (.-) (Num a b) (Num c d) = Num (a*d - c*b) (b*d)
  50.  
  51. (.*) :: Frac -> Frac -> Frac
  52. (.*) (Num a b) (Num c d) = Num (a*c) (b*d)
  53.  
  54. (./) :: Frac -> Frac -> Frac
  55. (./) (Num a b) (Num c d) = Num (a*d) (b*c)
  56.  
  57. -- Molimo, ne mijenjati kod ispod
  58. main :: IO()
  59. main = do i <- getLine
  60. let a = read i :: Int
  61. i <- getLine
  62. let b = read i :: Int
  63. i <- getLine
  64. let c = read i :: Int
  65. i <- getLine
  66. let d = read i :: Int
  67. print $ (Num a b) .+ (Num c d)
  68. print $ (Num a b) .- (Num c d)
  69. print $ (Num a b) .* (Num c d)
  70. print $ (Num a b) ./ (Num c d)
  71.  
  72.  
  73.  
  74. Zad 4.
  75. ----------------------------------
  76.  
  77. import Data.Char
  78. d2i xs = foldl(\a x -> 10*a + digitToInt x) 0 xs
  79. slova = "abcdefghijklmnopqrstuvwxyz"
  80. uniq [] = []
  81. uniq (x:xs) = [x] ++ uniq (filter (\e -> e /=x) xs)
  82. isWord xs = and [x `elem` slova | x <- xs]
  83. isnumb xs = and [x `elem` "0123456789" | x <- xs]
  84. same' xs ys = concat[['1'] | x <- xs , y <- ys , x == y]
  85. same x y = same' (uniq x) y
  86. f' lastword broj rj num = do if (d2i num) `mod` 2 == 1 then
  87. do rijec <- getLine
  88. if isWord rijec && length rijec == (d2i num) then
  89. f lastword (broj+1) (rj ++ (same lastword rijec))
  90. else
  91. f' lastword broj rj num
  92. else
  93. do rijec <- getLine
  94. if isWord rijec && length rijec == (d2i num) then
  95. f rijec (broj+1) rj
  96. else
  97. f' lastword broj rj num
  98.  
  99. f lastword broj rj = do num <- getLine
  100. if not (isnumb num) then
  101. f lastword broj rj
  102. else
  103. if d2i num == 0 then
  104. return (d2i rj)
  105. else
  106. if (d2i num) `mod` 2 == (broj `mod` 2) then
  107. f' lastword broj rj num
  108. else
  109. f lastword broj rj
  110.  
  111.  
  112. main :: IO()
  113. main = do sol <- f "" 0 ""
  114. print(sol)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement