Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.13 KB | None | 0 0
  1. --Write a program to add two numbers
  2. mysum a b = a + b
  3.  
  4. --Write a function to display the square of a number
  5.  
  6. mysqr a = a*a
  7.  
  8. --Write a function to read a number and display
  9. --True if even and false otherwise
  10.  
  11. isEven n = even n
  12.  
  13. isEve n = mod n 2==0
  14.  
  15. -- list comprehension
  16. -- [______option___|__conditions___]
  17. --1. display all even numbers from 1 to n
  18. --x is always be a new variable that is not the input side(n) non functional elements.
  19. --for x=1 to n vvvvvv
  20. --[ output | x<-[1..n], condition(if true, goto output) ]
  21. evens n = [ x | x<-[1..n], mod x 2==0 ]
  22.  
  23. --','means AND, if want to do OR, use or [....,....]
  24. --2. Both divisible by 2 and 3
  25. divisible n = [ x | x<-[1..n], mod x 2==0,mod x 3==0 ]
  26.  
  27. --3. Divisible by 2 OR 3
  28. divisible1 n = [ x | x<-[1..n], or [mod x 2==0,mod x 3==0] ]
  29.  
  30. --4. Given a list of values, display only the even numbers
  31. -- eg. dispevens [3,6,7,8] => [6,8]
  32. -- ??MUST DEFINE THE TYPE FIRST.
  33. --dispevens []=[]
  34. dispevens x = [ y | y<-x, mod y 2==0 ]
  35.  
  36. --5. Display all perfect divisors of a number n
  37. --eg: divisors 6 => [1,2,3,6]
  38.  
  39. divisors n = [x |x<-[1..n], mod n x==0]
  40.  
  41. --6. For a given number n, display True if prime and False if non-prime.
  42. --eg: isPrime 7 =>True
  43. --Prime? a number which can only be divide by 1 and itself.
  44.  
  45. --isPrime n= if(length (divisors n))==2 then True else False
  46. isPrime n= length (divisors n)==2
  47. isPrime1 n= divisors n==[1,n]
  48.  
  49. --Display a list of all prime numbers from 1 to n.
  50. --eg: disprimes 10 =>[2,3,5,7]
  51. disprimes n= [ x|x<-[1..n], isPrime1 x==True]
  52.  
  53. --Display the cubes of all numbers from m to n.
  54. --eg: cubes 2 5 =>[8,27,64,125]
  55. cubes a b = [ x^3| x<-[a..b]]
  56.  
  57. --Given a list of numbers, display the sum of positive values
  58. --eg: sumList [1,-3,4,-5]=>5
  59.  
  60. readList1 m=[x|x<-m, x>=0]
  61. sumList a=sum (readList1 a)
  62.  
  63.  
  64. --Given a list of values, display True if all values are prime and False otherwise.
  65. --Eg: areprimes [2,7,13,17] =>True
  66.  
  67. areprimes m=and [ isPrime x|x<-m ]
  68.  
  69. --Given a list of values, Multiply each value with its index position starting from 1. Eg: fun [4,8,0,1] => [4,16,0,4]
  70. --QUESTION CANCELLED.
  71.  
  72. --EXAM: STUDY THE QUESTIONS GIVEN.
  73. --NEW LIST COMPREHENSIONS~~~TUPLES~~~
  74. --why is it connected to database.
  75. --can have a combination of all different datatype. ex: (1,"sam",25)
  76. --tuples are more RESTRICTED. example: length [(1,"sam"), (2,"mary",40)] will have error.
  77. --EXAM! remember this sample, and reason of its pattern!.
  78. --Nested FOR LOOP. for x=1 to 3
  79. -- {for y=5 to 7
  80. -- print (x,y)}
  81. sample =[(x,y) | x<-[1..3],y<-[5..7]]
  82.  
  83. --1. Given 2 lists. Display the product terms of every value in the first list with that of the second list.
  84. --eg: prodlists [1,2] [2,3,4] => [2,3,4,4,6,8]
  85.  
  86. prodlists a b=[(x*y)|x<-a,y<-b]
  87.  
  88. --2. Display all pythagorean triplets from 1 to n
  89. --eg: pytg 25=> [(4,3,5),(8,6,10),(12,5,13)...(20,15,25)]
  90.  
  91. pytg a=[(x,y,z)|x<-[1..a],y<-[1..a],z<-[1..a],x^2+y^2==z^2,x<y]
  92.  
  93.  
  94. --Comprehension
  95. {-Give a list of tuples. Display the id, length of the name and marks>=50
  96. Eg. sortout [(1,"sam",98),(2,"alex",45),(3,"maria",78)]
  97. OUTPUT=> [(1,3,98),(3,5,78)]
  98. -}
  99.  
  100. sortout t=[(x,length y,z)|(x,y,z)<-t,z>=50]
  101.  
  102. {-
  103. Given a list of tuples. Display the squares of the values in the tuples, provided they are in ascending order.
  104. Eg. listout [(2,5),(7,2),(3,6)]=> [(4,25),(9,36)]
  105. -}
  106.  
  107. listout y=[(a^2,b^2) |(a,b)<-y,a<=b]
  108.  
  109.  
  110. {-~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. Recursion (Iteration-keep repeating. for loop but the repetition has a order. Example: Family tree.)
  112. curry list. ->handle the remaining variable.
  113.  
  114. lst (a:b:c:ds) =
  115. ^this pattern TELLS THAT IT IS A 100% LIST.
  116. Example: [2,3,4,5,6,7,8,9] a=2, b=3, c=4, ds=5~9
  117. the list provided minimum value must be 3! because got a,b,c
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~-}
  119.  
  120. --Write a function that displays the first element in a list.
  121. --DONT USE HEAD.
  122.  
  123. --myhead (a)=a
  124. myhead (a:b)=a
  125.  
  126. --Write a function that displays the all elements in a list except the first element. DONT USE TAIL.
  127.  
  128. mytail (a:b)=b
  129.  
  130. {-~~~~~~~~~~~~~~~~~~~~~~~
  131. Example ::Factorial
  132. 5!=5x4!
  133. 4!=4x3!
  134. ...
  135. 1!=1
  136. n: 1x2x3x4...xn.
  137. Start/Stopping condition
  138. Recursive rule
  139. n!=nx(n-1)!
  140. 1!=1 (points to stop)
  141. ~~~~~~~~~~~~~~~~~~~~~~~~-}
  142. --Stopping criteria should always be at the top
  143. --SIMPLE RECURSION
  144. fact 1=1
  145. fact 0=1
  146. --generic pattern at below
  147. fact n=n*fact (n-1)
  148.  
  149. --Fibonacci
  150. --MULTILEVEL RECURSION
  151. --stopping criteria
  152. fib 1=0
  153. fib 2=1
  154. --rule v
  155. fib n= fib (n-1)+fib(n-2)
  156.  
  157.  
  158. --write a function to add all numbers in a list
  159. --dont use sum function in your answer
  160.  
  161. msum [] = 0
  162. msum (x:xs) = x + msum xs
  163.  
  164. --write a function to display the last element in a list
  165. -- dont use last function in your answer
  166.  
  167. mylast [] = 0
  168. mylast [x] = x
  169. mylast (x:xs) = mylast xs
  170.  
  171. mylas (x:xs) = if(xs==[]) then x else mylas xs
  172.  
  173. --rewrite the init function
  174.  
  175. myinit [x] = []
  176. myinit (x:xs) = [x]++ myinit xs
  177.  
  178. --disply the nth element in a list
  179. --disp 3[4,3,5,6,7,8,]=>5
  180.  
  181. disp 1(x:xs) = x
  182. disp n(x:xs) = disp (n-1) xs
  183.  
  184. --display the largest value in a list
  185.  
  186. dislar [x] = x
  187. dislar (x:y:zs) = if(x>y) then dislar ([x]++zs) else dislar (y:zs)
  188.  
  189. --reverse a list of values
  190.  
  191. rev [] = []
  192. rev (a:as) = rev as++[a]
  193.  
  194. --check if a list is a palidrome
  195.  
  196. chkpalin xs = xs==rev(xs)
  197.  
  198. --given a list, determine if it is sorted in the ascending order
  199. issorted [] = True
  200. issorted [x] = True
  201. issorted (x:y:zs) = if(x<y) then issorted (y:zs) else False
  202.  
  203. myzip xs [] = []
  204. myzip [] ys = []
  205. myzip (x:xs)(y:ys) = [(x,y)]++ myzip xs ys
  206.  
  207.  
  208. {-~~~~~~~~~~~~~~Tutorial 29 November~~~~~~~~~~~~~~-}
  209.  
  210. --Write a function to display the last element in a list.
  211. --dont use Last function
  212.  
  213. mylast1 []=0
  214. mylast1 [x]=x
  215. mylast1 (x:xs) = mylast1 xs
  216.  
  217.  
  218. --Write a function to display the sum of the squares of numbers in a list.
  219. --Eg: mysum [1,4,2] = 21 (1^2+4^2+2^2=21) Dont use sum function
  220.  
  221. mysum3 []=0
  222. mysum3 (x:xs)=x^2 + mysum3 xs
  223.  
  224. --Write a function to display the smallest value in a list.
  225. --Eg: mysmall [1,0,1,0,2,1,3,0]=0
  226.  
  227. mysmall [x]=x
  228. -- no nid this ->mysmall [x,y]=if(x<y) then x else y
  229. mysmall (x:y:xs)=if(x<y) then mysmall (x:xs) else mysmall (y:xs)
  230.  
  231.  
  232. --Write a function to display the smallest value in a list INCLUDING REPETITION.
  233. --Eg: allsmall [1,0,1,0,2,1,3,0]=[0,0,0]
  234.  
  235. allsmall xs=[x|x<-xs,x==mysmall xs]
  236.  
  237. {-~~~~~~~~~~~~~~~~Selection Sort~~~~~~~~~~~~~~~~~~-}
  238. --reuse allsmall and mysmall function
  239.  
  240. notsmall xs=[x|x<-xs,x/=mysmall xs]
  241. selsort []=[]
  242. selsort xs=allsmall xs++selsort (notsmall xs)
  243.  
  244. --Another answer
  245. selsort1 []=[]
  246. selsort1 xs=allsmall xs ++ selsort [x|x<-xs,x>mysmall xs]
  247.  
  248.  
  249. {-~~~~~~~~~~~~~~~~~Quick Sort~~~~~~~~~~~~~~~~~~~~~~-}
  250. --compare the x, check with xs. if smaller, put in [] front x, else larger, put into [] after x
  251. --eg:[5,3,7,2,6,0]
  252.  
  253. quickSort []=[]
  254. quickSort [x]=[x]
  255. quickSort (x:xs)=quickSort [y|y<-xs,y<=x]++[x]++ quickSort [z|z<-xs,z>x]
  256.  
  257.  
  258. --Write a function to decompose a number into digits. Eg: decom 5368=[8,6,3,5]
  259.  
  260. decom 0=[]
  261. decom x=decom (div x 10) ++[mod x 10]
  262.  
  263.  
  264. {-2% will come out in test-}
  265. {-~~~~~~~~~~~~~~~~~Merge Sort~~~~~~~~~~~~~~~~~~~~~~-}
  266. --Write a function 'merge', merging two SORTED LIST together.
  267. --example: [1,7,9,10] [2,4,5,6]
  268.  
  269.  
  270. merge [] x=x
  271. merge x [] =x
  272. merge (x:xs) (y:ys)= if(x<=y) then [x]++ merge xs (y:ys) else [y]++ merge (x:xs) ys
  273.  
  274. --Mergesort
  275.  
  276. msort [x]=[x]
  277. msort [x,y]=if (x<=y) then [x,y] else [y,x]
  278.  
  279. msort xs=merge(msort(take (div (length xs) 2) xs)) (msort(drop (div (length xs) 2) xs))
  280.  
  281. {-~~~~~~~~~~~~~~~~~~~~Lab Test covered in question!!~~~~~~~~~~~~~~~~~~~~~~~~~~~`-}
  282.  
  283.  
  284. --Decison Making
  285.  
  286. -- if <exp> then <op1> else <op2>
  287.  
  288. -- largest of 2 numbers
  289.  
  290. --lar a b = if(a>b) then a
  291.  
  292. --like this will have error, because then must has a "ELSE" ^
  293.  
  294. lar1 a b = if(a>b) then a else b
  295.  
  296. --The problem is then and if must produce same DATA TYPE! why? because it is a function, it will return SAME data type! ex: function int main(); because the intepretor is defining for u, so we need to give the same data type!
  297.  
  298. --lar a b= if(a>b) then "first" else b
  299.  
  300. lar a b= if(a>b) then "first" else "second"
  301.  
  302. --Display the largest of three numbers
  303.  
  304. largest a b c= if(a>b&&a>c) then show a ++ " is largest"
  305. else if(b>c) then show b++" is largest"
  306. else show c++" is largest"
  307.  
  308. largest1 a b c= lar1 (lar1 a b) c
  309.  
  310. --1. Read 3 numbers and determine if they are pythagorean triplets(a2 + b2 is equal = c2).
  311.  
  312. pytha a b c= if((a^2)+(b^2)==c^2) then "Is pythagorean triplets" else "Not pythagorean triplets"
  313.  
  314. --2. Display the absolute value of a number.(1 number input, what is absolute? if input is -5, make it to 5. Convert everyinput to positive. If is positive then make it unchanged) testing must put : absolute (-7), because need to show that it is a single input NOT 2 argument
  315.  
  316. absolute a=if(a<0) then (-a) else a
  317.  
  318. --3. For given two numbers, determine if they divide each other perfectly(one way then is true).
  319.  
  320. perfect a b=if((mod a b)==0) then "true" else if((mod b a)==0) then "true" else "false"
  321.  
  322. perfect1 a b=or [mod a b==0, mod b a==0]
  323.  
  324. --4. Display the sinc of a number. sinc x = sin x/x. But sinc 0 is 1. (Sinc function is .. is x is 0 degree, means it is 1. sinc 0 degree=0,) 0/0= undefined, 1/0=infinity(any num divide by 0 is infinity)
  325.  
  326. sinc a = if(a==0) then 1 else (sin a)/a
  327.  
  328.  
  329. --5. Read a 3digit number n. Determine if it is Armstrong number(example: 153, 153=1^3+5^3+3^3). How to decompose 3digit number.
  330.  
  331. -------------------last---------middle-------------first num--
  332. armstrong1 a= if(mod a 10)^3+(mod(div a 10) 10)^3+(div a 100)^3==a then "true" else "false"
  333.  
  334. --mod->get the remainder (divide by 10 will get the remainder)
  335. --div->get the answer (divide by 100 will get the 1st number)
  336. --Middle?divide by 10, get the 15, then mod 10 to get the remainder
  337. --_______________________________________________________________
  338. --Tutorial 3
  339. --_______________________________________________________________
  340. --Normal Function
  341. --funname a1 a2 a3 = exp
  342.  
  343. {-Guarded Expressions(SwitchCase), if the condition is true
  344. funname a1 a2 a3 |<condition1> = <exp1>
  345. |<condition2> = <exp2>
  346. |<condition3> = <exp3>
  347. -}
  348.  
  349. --Largest of two numbers (otherwise = Default)
  350. largest2 x y|(x>=y)=x
  351. |(y>=x)=y
  352. |otherwise=y
  353.  
  354. --determine the smallest of 3 numbers
  355. smallest a b c|(a<b&&a<c)=a
  356. |(b<c)=b
  357. |otherwise=c
  358.  
  359. {-
  360. *Pattern Matching / Polymorphism*
  361. 2 Categories: Adhoc(OOP-Java, C++, C#) & Parametric(Haskell, Python)
  362.  
  363. -Adhoc-
  364. ~have more than 1 signature
  365. Essential to define polymorphism: same name for function, number of argument, different parameter and different datatype.
  366. ~Behave differently for same sum function.
  367.  
  368. -Parametric-
  369. ~can have only 1 signature
  370. ~Cannot change the skeleton(the datatype returned, the datatype entered.)
  371. -define more than 1 version of the same function.
  372. Example:
  373. 1. myand True True = True ::Pattern matching can be perform. Variable data is given.
  374. 2. myand a b = False
  375.  
  376. If called :"myand True False", will get 'false' because True False !=True True.
  377. Check the generic pattern after the SPECIFIC PATTERN.
  378. -}
  379.  
  380. myand True True = True
  381. --the a b can only be Boolean, because the previous pattern is Boolean!!the skeleton already formed. :)
  382. myand a b =False
  383.  
  384. --Implement sinc function using pattern matching
  385. --sinc of 0 is 1
  386. --sinc of x is (sin x)/x
  387.  
  388. sinc1 0=1
  389. sinc1 x=(sin x)/x
  390.  
  391. --The variable name of all type of data is the same.
  392. --Implement a function called safetail. This function returns an empty list if the input is an empty list.
  393.  
  394. func1 a=a+1
  395. func2 a=length a
  396. func3 a=(a==True)
  397.  
  398. safetail1 a=if (length a==0)then [] else tail a
  399.  
  400. list []=[]
  401. --x is the only number in the list
  402. list [x]=[x]
  403. list [x,y]=[x,y]
  404. list a=tail a
  405.  
  406. --Write a function that accepts a list of values. If the list has one or two or three elements do the following:
  407. --a
  408. --a+b
  409. --a-b+c
  410. --For more number of elements display their product.
  411.  
  412. listFunc [x]=x
  413. listFunc [x,y]=x+y
  414. listFunc [x,y,z]=x-y+z
  415. --practice: as = give an infomation that it represents an List.
  416. listFunc as=product as
  417.  
  418. --rewrite the product behavious such that the product of an empty list is 0.
  419. productEmpty []=0
  420. productEmpty as=product as
  421.  
  422. --Read a list of values. For a list of upto 3 values(first~third) display the sorted list. If there are more than 3 values, display as empty list.
  423.  
  424. sorted [x] = [x]
  425. sorted [x,y]|(x>=y)=[y,x]
  426. |otherwise=[x,y]
  427. sorted [x,y,z]= if(y<z) then [y,z] else [z,y]
  428. sorted xs =[]
  429.  
  430. sortThree [x,y,z]=if(x<=y&&x<=z) then if(y<=z) then [x,y,z] else [x,z,y] else if(y<=x&&y<=z) then if(x<=z) then [y,x,z] else [y,z,x] else if(x<=y) then [z,x,y] else [z,y,x]
  431.  
  432.  
  433. ------------------------------------------------------------------------------------------------
  434. /* John and Mary had two children named Alex and Susan.
  435. Alex married Diana. They had a child named Leo.
  436. Susan adopted two children named Philip and Daniel.
  437.  
  438. Represent this information using suitable functors in your knowledge base.
  439. */
  440.  
  441. father(john,alex).
  442. father(john,susan).
  443. mother(mary,alex).
  444. mother(mary,susan).
  445. father(alex,leo).
  446. mother(diana,leo).
  447. mother(susan,philip).
  448. mother(susan,daniel).
  449.  
  450. /*
  451. Write rules to determine the following information.
  452. 1. Display the children given the name of father or mother.
  453. 2. Determine if two people may be siblings.(same parent)
  454. 3. Who are the parents of Daniel.
  455. 4. Who is the husband of Susan?
  456. */
  457. %Question 1
  458. par_chi(A,B):-mother(A,B);father(A,B).
  459.  
  460. %Question 2
  461. isSibling(A,B):-par_chi(C,A),par_chi(C,B).
  462.  
  463. %Question 3 just run by "par_chi(A,daniel).
  464.  
  465. %Question 4-(write a generic rule.)
  466. couple(A,B):-par_chi(A,C),par_chi(B,C).
  467.  
  468. %EXAM!!!Memorize the code
  469. % Recurrsion in Prolog.
  470. parent(john,alex).
  471. parent(alex,mary).
  472. parent(mary,peter).
  473. parent(peter,susan).
  474.  
  475. %Stopping criteria.
  476. anc(A,B):-parent(A,B).
  477.  
  478. %Recussion, if A not B's parent, find A's parent.
  479. anc(A,B):-parent(X,B),anc(A,X).
  480.  
  481. %==================================%
  482. %No exam! List Comprehension. For knowledge.
  483. mylist(A,[A|T]).
  484. %mylist([sam, john, alex]).
  485. % A=sam.
  486. mylist(T,[A|T]).
  487. %mylist(A,[sam, john, alex]).
  488. % A=[john, alex].
  489.  
  490. isfound(A,[A|T]).
  491. isfound(A,[H|T]):-isfound(A,T).
  492.  
  493. friends([yemay,caifu,julius,alison,jay,samiha,yeesheng]).
  494.  
  495. isfriend(A,B):-friends(X),isfound(A,X),isfound(B,X).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement