vakho

Haskell - 12.12.13

Dec 11th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- let (local variables for functions)
  2. myLetFunc a b c =   let det = sqrt(b^2 - 4*a*c)
  3.             in ((-b + det) / (2*a), (-b - det) / (2*a))
  4.  
  5. -- where
  6. myWhereFunc a b c =     ((-b + det) / twice_a, (-b-det) / twice_a)
  7.             where   det = sqrt (b*b-4*a*c)
  8.                 twice_a = 2*a
  9.  
  10. -- dacva
  11. factorial 0 = 1
  12. factorial n     | n < 0 = error "factorial: negative argument"
  13.         | n >= 0 = n * factorial (n-1)
  14.  
  15. signum x    | x < 0 = -1
  16.         | x > 0 = 1
  17.         | otherwise = 0
  18.  
  19. -- data
  20. data Color = Red | Green | Blue | RGB Int Int Int
  21. data Bool = True | False
  22.  
  23. {-data a = Nothing | Just a
  24. gayofa 0 b = Nothing
  25. gayofa a b = Just (a / b)-}
  26.  
  27. data Daytime = Morning | Afternoon | Evening | Night deriving (Eq, Show)
  28.  
  29. -- Book Store
  30. data Product = Book String String | Video String | Cd String String Int
  31.  
  32. getTitle :: Product -> String
  33. getTitle (Book a b) = a
  34. getTitle (Video a) = a
  35. getTitle (Cd a b c) = a
  36.  
  37. getTitles :: [Product] -> [String]
  38. getTitles [] = []
  39. getTitles (x:xs) = getTitle x : getTitles xs
  40.  
  41. bookAuthors :: [Product] -> [String]
  42. bookAuthors [] = []
  43. bookAuthors ((Book a b):xs) = a : bookAuthors(xs)
  44.  
  45. lookupTitle :: String -> [Product] -> Maybe Product
  46. lookupTitle str [] = Nothing
  47. lookupTitle str (x:xs) = if (getTitle x == str) then Just x else lookupTitle str xs
  48.  
  49. lookupTitles :: [String] -> [Product] -> [Maybe Product]
  50. lookupTitles [] _ = []
  51. lookupTitles _ [] = []
  52. lookupTitles (s:str) xs = lookupTitle s xs : lookupTitles str xs
  53.  
  54. -- Trees
  55. data Tree a = Leaf a | Branch (Tree a) (Tree a)
  56.  
  57. treeSize (Leaf _) = 1
  58. treeSize (Branch l r) = treeSize l + treeSize r
  59.  
  60. leafList (Leaf x) = [x]
  61. leafList (Branch l r) = leafList l ++ leafList r
  62.  
  63. -- magali rigis funkciebi
  64. -- foldr (-) 0 [1,2,3] daabrunebs (1-(2-(3-0))) = 2
  65. -- foldl (-) 0 [1,2,3] daabrunebs (((0-1)-2)-3) = -6
  66.  
  67. listAverage xs = foldr (+) 0 xs / (sizeOfList xs)
  68. sizeOfList [] = 0
  69. sizeOfList (x:xs) = sizeOfList xs + 1
  70.  
  71. skalarOfLists xs ys = foldr (+) 0 (zipWith (*) xs ys)
  72.  
  73. countEven xs = filter even xs
  74.  
  75. quicksort                 ::    Ord a => [a] -> [a]
  76. quicksort []        =   []
  77. quicksort (p:xs) =      (quicksort lesser) ++ [p] ++ (quicksort greater)
  78.                 where
  79.                                 lesser  = filter (< p) xs
  80.                                 greater = filter (>= p) xs
  81.  
  82. quicksort2 p (xs) = [x | x<-quicksort xs, p x]
  83. quicksort3 p xs = filter p (quicksort xs)
  84.  
  85. -- map aketebs moqmedebas siis yvela elementistvis
  86.  
  87. doJami xs = map (+1) xs
  88.  
  89. --gamosaxet [f x | x<xs, px] map-it da filter-it
  90. mySuperFunc p f xs = map f (filter p xs)
  91.  
  92. -- type (tipis gamocxadeba)
  93. type String = [Char]
  94.  
  95. type Pos = (Int, Int)
  96.  
  97. origin :: Pos
  98. origin = (0,0)
  99.  
  100. type Pair a = (a, a)
  101.  
  102. mult :: Pair Int -> Int
  103. mult (m, n) = m*n
  104.  
  105. -- binaruli xe
  106. data Tree = Leaf Int | Node Tree Int Tree
  107.  
  108. occurs :: Int -> Tree -> Bool
  109. occurs a (Leaf b) = (a==b)
  110. occurs a (Node l b r) = ((a==b) || (occurs a l) || (occurs a r))
  111.  
  112. flatten :: Tree -> [Int]
  113. flatten (Leaf n) = [n]
  114. flatten (Node l n r) = (flatten l) ++ [n] ++ (flatten r)
Advertisement
Add Comment
Please, Sign In to add comment