ElenaR1

4 upr

Jan 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Dali duljinata na vseki string = elementa ot drugiq spisuk ["abc","cdef","a"] [3,4,1]-da
  2. func::[String]->[Int]->Bool
  3. func [] []=True
  4. func (x:xs) (y:ys)
  5.   |length x==y=func xs ys
  6.   |otherwise=False
  7.  
  8.  
  9. --countOccurences [1,2,4,5,2,3,2] 2->3
  10. countOccurences::(Eq a)=>[a]->a->Int
  11. countOccurences xs a=countOccurencesHelper xs a 0
  12. countOccurencesHelper::(Eq a)=>[a]->a->Int->Int
  13. countOccurencesHelper [] a count =count
  14. countOccurencesHelper (x:xs) a count
  15.   |x==a=countOccurencesHelper xs a (count+1)--
  16.   |otherwise=countOccurencesHelper xs a count
  17.  
  18. countOccurences::(Eq a)=>[a]->a->Int
  19. countOccurences [] elem=0
  20. countOccurences (x:xs) elem
  21.   |x==elem=1+countOccurences xs elem
  22.   |otherwise=countOccurences xs elem
  23.  
  24.  
  25. myFilter::(a->Bool)->[a]->[a]
  26. myFilter f []=[]
  27. myFilter f (x:xs)
  28.   |f x==True= x: myFilter f xs  
  29.   |otherwise= myFilter f xs  
  30.  
  31. type Matrix a=[[a]]
  32. type Column a=[a]
  33. --vzima kolona
  34. nthCol::Matrix Int->Int->Column Int
  35. nthCol [] n=[]
  36. nthCol xs n=map (!!n) xs
  37. --vzima red
  38. type Row a=[a]
  39. nthRow:: Matrix Int->Int->Row Int
  40. --nthRow xs n=xs!!n
  41. nthRow =(!!)
  42.  
  43.  
  44. any'::(a->Bool)->[a]->Bool
  45. any' f []=False
  46. any' f (x:xs)
  47.  |f x==True=True
  48.  |f x==False=any' f xs
  49.  
  50.  
  51. any'::(a->Bool)->[a]->Bool
  52. any' f xs=or(map f xs)
  53. --stava kato kompozicia ot funkcii map vrushta (F,T,T)
  54. or(F,T,T) vrushta True t.k imame pone 1 true
  55.  
  56.  
  57. --all
  58. all'::(a->Bool)->[a]->Bool
  59. all' f xs=and(map f xs)
  60.  
  61. --split
  62. split::[a]->Int->([a],[a])
  63. split xs n=(take  n xs,drop n xs)
  64.  
  65.  
  66. --group
  67. group'::(Eq a)=>[a]->[[a]]
  68. group' []=[]
  69. group' (x:xs)=takeWhile (==x) (x:xs) : group' (dropWhile (==x) (x:xs))
  70.  
  71. --group'::(Eq a)=>[a]->[[a]]
  72. --group' []=[]
  73. --group' (x:xs)=[takeWhile (==x) (x:xs)] ++ group' (dropWhile (==x) (x:xs))
  74.  
  75. --minElement
  76. minElement::(Ord a)=>[a]->a
  77. minElement (x:xs)=minElementHelper xs x
  78. minElementHelper::(Ord a)=>[a]->a->a
  79. minElementHelper[] min=min
  80. minElementHelper (x:xs) min
  81.   |x<min=minElementHelper xs x
  82.   |otherwise=minElementHelper xs min
  83.  --(expr,expr)
  84.  
  85. minElement::(Ord a)=>[a]->a
  86. minElement (x:xs)=foldl (min) x xs
  87.  
  88. --remove
  89. remove::(Eq a)=>a->[a]->[a]
  90. remove n []=[]
  91. remove n (x:xs)
  92.   |x==n=xs
  93.   |otherwise=x:remove n xs  
  94.  
  95.  
  96. --sort
  97. --da polzvam min i remove
  98. minElement::(Ord a)=>[a]->a
  99. minElement (x:xs)=minElementHelper xs x
  100. minElementHelper::(Ord a)=>[a]->a->a
  101. minElementHelper[] min=min
  102. minElementHelper (x:xs) min
  103.   |x<min=minElementHelper xs x
  104.   |otherwise=minElementHelper xs min
  105.  
  106.  
  107. remove::(Eq a)=>a->[a]->[a]
  108. remove n []=[]
  109. remove n (x:xs)
  110.   |x==n=xs
  111.   |otherwise=x:remove n xs  
  112.  
  113. sort'::(Ord a)=>[a]->[a]
  114. sort' []=[]
  115. sort' xs=(minElement xs):sort' (remove (minElement xs) xs)
  116.  
  117.  --(expr,expr)
  118.  
  119.  
  120. --8 github
  121. remove::(Eq a)=>a->[a]->[a]
  122. remove n []=[]
  123. remove n (x:xs)
  124.   |x==n=xs
  125.   |otherwise=x:remove n xs  
  126.  
  127. sortBy::(Ord a)=>(a->a->a)->[a]->[a]
  128. sortBy f []=[]
  129. sortBy f xs=minElement:sortBy f (remove minElement xs)
  130.   where minElement=foldl f (head xs) xs
  131.  
  132.  
  133. --foldl min (head [3,1,5,2]) [3,1,5,2]->1
  134. --foldl func 4 [4,3,1,2]->1
  135. func::(Ord a)=>a->a->a
  136. func x y
  137.   |x<y=x
  138.   |otherwise=y
  139.  
  140. --po-obshto reshenie
  141. remove::(Eq a)=>a->[a]->[a]
  142. remove n []=[]
  143. remove n (x:xs)
  144.   |x==n=xs
  145.   |otherwise=x:remove n xs  
  146.  
  147. -- minl (\x y -> length x < length y) ["strings","about","to","get","sorted"]->"to"
  148. minl::(Ord a)=>(a->a->Bool)->[a]->a
  149. minl f (x:xs)=minLen f x xs
  150.  
  151. minLen::(a->a->Bool)->a->[a]->a
  152. minLen f min []=min
  153. minLen f min (x:xs)
  154.   |f x min==True=minLen f x xs
  155.   |otherwise=minLen f min xs
  156.  
  157. sortBy::(Ord a)=>(a->a->Bool)->[a]->[a]
  158. sortBy f []=[]
  159. sortBy f xs=(minl f xs):sortBy f (remove (minl f xs) xs)
  160.  
  161.  
  162. --9 github
  163. replicate'::[a]->Int->[a]
  164. replicate' xs n=replicatef xs n n
  165.  
  166. replicatef::[a]->Int->Int->[a]
  167. replicatef [] n originalN=[]
  168. replicatef (x:xs) n originalN
  169.   |n==0=replicatef xs originalN originalN
  170.   |otherwise=x: replicatef (x:xs) (n-1) originalN
  171.  
  172.  
  173.  
  174.  
  175.  
  176. --16.01
  177. --[1,2,3,4,5]->[[1,2],[3,4],[5]]
  178. lis2::[Int]->[[Int]]
  179. lis2 []=[]
  180. lis2 [x]=[[x]]--ne moje da e samo [x] t.k ako ostava samo edin element ne stigame do prazniq spisuk i shte stane [1,2]:[3,4]:[5] koeto -----nishto ne dava
  181. --lis2 (x:y:xs)=[x:y:[]]++lis2 xs
  182. --lis2 (x:y:xs)=(x:y:[]):lis2 xs--moje samo [x,y] vmesto (x:y:[])
  183. lis2 xs=[take 2 xs]++lis2 (drop 2 xs)
  184.  
  185.  
  186. http://zvon.org/other/haskell/Outputprelude/foldl_f.html
  187. func::[(Int,Int)]->(Int,Int)
  188. func xs=(minimum (map fst xs),maximum (map snd xs))
  189. -- [(1,5), (6, 50)...]->(1,50) minimalnata 1va chast na 2ka i maksimalnata 2ra chast
  190.  
  191.  
  192.  
  193. preff::[a]->[[a]]
  194. preff xs=[take x xs| x<-[1..length xs]]
  195. --[1,2,3]->[[1],[1,2],[1,2,3]]
  196. suff::[a]->[[a]]
  197. suff xs=[drop x xs| x<-[0..length xs-1]]
  198. --[1,2,3]->[[1,2,3],[2,3],[3]]
  199.  
  200.  
  201. f::[a]->[[a]]
  202. f xs=[take x xs| x<-[length xs,length xs-1..1]]
  203. --[1,2,3]->[[1,2,3],[1,2],[1]]
  204.  
  205.  
  206.  
  207. sublist::(Eq a)=>[a]->[a]->Bool
  208. sublist xs []=False
  209. sublist xs ys=elem xs (preff ys)|| elem xs (suff ys)||sublist xs (tail ys)
  210. --sublist xs ys=
  211. --dali [2,3] e v [1,2,3,4]->true
  212.  
  213.  
  214.  
  215. 18.01
  216. permutations::(Eq a) => [a]->[[a]]
  217. permutations[]=[[]]
  218. --permutations[x]=[[x]]
  219. --permutations [x,y]=[[x,y],[y,x]]
  220. permutations xs=[i:j|i<-xs,j<-permutations(delete xs i)]
  221. --[x:ys|x<-xs,ys<-perm(deletex xs)]
  222. delete::(Eq a)=>[a]->a->[a]
  223. delete [] z=[]
  224. delete (x:xs) z
  225.   |x==z=xs
  226.   |otherwise=x:delete xs z
  227.  
  228. grayCode::Int->[[Int]]
  229. grayCode 0=[[]]
  230. grayCode x=(map (0:) lessGray)++ (map (1:) lessGray)
  231.   where lessGray=grayCode(x-1)
  232.  
  233. --GoldBach Every even integer greater than 2 can be expressed as the sum of two primes.[
  234. prime::Int->Bool
  235. prime 1=False
  236. prime 2=True
  237. prime x
  238.   |null [i|i<-[2,3..x-1],divides x i]=True
  239.   |otherwise=False
  240.  
  241. divides::Int->Int->Bool
  242. divides x y
  243.   |x`mod` y==0=True
  244.   |otherwise=False
  245.  
  246.  
  247. --returns a list of prime numbers
  248. generatePrimes::Int->[Int]
  249. generatePrimes max=[i|i<-[2..max],prime i]
  250.  
  251. --or we can do this in this way but it doesn't work
  252. --primeToList::Int->[Int]
  253. --primeToList max=primeToListHelper max 1 []
  254.  
  255. --primeToListHelper::Int->Int->[Int]->[Int]
  256. --primeToListHelper max start resultList
  257. --  |start>max=resultList
  258. --  |prime start==True=primeToListHelper max (start+1) start:resultList
  259. --  |otherwise=primeToListHelper max (start+1) resultList
  260.  
  261. goldhelp::Int->[(Int,Int)]
  262. goldhelp n = [(x,y) | x <- pr, y <- pr, x+y==n]
  263.   where pr=generatePrimes n
Add Comment
Please, Sign In to add comment