Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Write a program to add two numbers
- mysum a b = a + b
- --Write a function to display the square of a number
- mysqr a = a*a
- --Write a function to read a number and display
- --True if even and false otherwise
- isEven n = even n
- isEve n = mod n 2==0
- -- list comprehension
- -- [______option___|__conditions___]
- --1. display all even numbers from 1 to n
- --x is always be a new variable that is not the input side(n) non functional elements.
- --for x=1 to n vvvvvv
- --[ output | x<-[1..n], condition(if true, goto output) ]
- evens n = [ x | x<-[1..n], mod x 2==0 ]
- --','means AND, if want to do OR, use or [....,....]
- --2. Both divisible by 2 and 3
- divisible n = [ x | x<-[1..n], mod x 2==0,mod x 3==0 ]
- --3. Divisible by 2 OR 3
- divisible1 n = [ x | x<-[1..n], or [mod x 2==0,mod x 3==0] ]
- --4. Given a list of values, display only the even numbers
- -- eg. dispevens [3,6,7,8] => [6,8]
- -- ??MUST DEFINE THE TYPE FIRST.
- --dispevens []=[]
- dispevens x = [ y | y<-x, mod y 2==0 ]
- --5. Display all perfect divisors of a number n
- --eg: divisors 6 => [1,2,3,6]
- divisors n = [x |x<-[1..n], mod n x==0]
- --6. For a given number n, display True if prime and False if non-prime.
- --eg: isPrime 7 =>True
- --Prime? a number which can only be divide by 1 and itself.
- --isPrime n= if(length (divisors n))==2 then True else False
- isPrime n= length (divisors n)==2
- isPrime1 n= divisors n==[1,n]
- --Display a list of all prime numbers from 1 to n.
- --eg: disprimes 10 =>[2,3,5,7]
- disprimes n= [ x|x<-[1..n], isPrime1 x==True]
- --Display the cubes of all numbers from m to n.
- --eg: cubes 2 5 =>[8,27,64,125]
- cubes a b = [ x^3| x<-[a..b]]
- --Given a list of numbers, display the sum of positive values
- --eg: sumList [1,-3,4,-5]=>5
- readList1 m=[x|x<-m, x>=0]
- sumList a=sum (readList1 a)
- --Given a list of values, display True if all values are prime and False otherwise.
- --Eg: areprimes [2,7,13,17] =>True
- areprimes m=and [ isPrime x|x<-m ]
- --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]
- --QUESTION CANCELLED.
- --EXAM: STUDY THE QUESTIONS GIVEN.
- --NEW LIST COMPREHENSIONS~~~TUPLES~~~
- --why is it connected to database.
- --can have a combination of all different datatype. ex: (1,"sam",25)
- --tuples are more RESTRICTED. example: length [(1,"sam"), (2,"mary",40)] will have error.
- --EXAM! remember this sample, and reason of its pattern!.
- --Nested FOR LOOP. for x=1 to 3
- -- {for y=5 to 7
- -- print (x,y)}
- sample =[(x,y) | x<-[1..3],y<-[5..7]]
- --1. Given 2 lists. Display the product terms of every value in the first list with that of the second list.
- --eg: prodlists [1,2] [2,3,4] => [2,3,4,4,6,8]
- prodlists a b=[(x*y)|x<-a,y<-b]
- --2. Display all pythagorean triplets from 1 to n
- --eg: pytg 25=> [(4,3,5),(8,6,10),(12,5,13)...(20,15,25)]
- pytg a=[(x,y,z)|x<-[1..a],y<-[1..a],z<-[1..a],x^2+y^2==z^2,x<y]
- --Comprehension
- {-Give a list of tuples. Display the id, length of the name and marks>=50
- Eg. sortout [(1,"sam",98),(2,"alex",45),(3,"maria",78)]
- OUTPUT=> [(1,3,98),(3,5,78)]
- -}
- sortout t=[(x,length y,z)|(x,y,z)<-t,z>=50]
- {-
- Given a list of tuples. Display the squares of the values in the tuples, provided they are in ascending order.
- Eg. listout [(2,5),(7,2),(3,6)]=> [(4,25),(9,36)]
- -}
- listout y=[(a^2,b^2) |(a,b)<-y,a<=b]
- {-~~~~~~~~~~~~~~~~~~~~~~~~~~
- Recursion (Iteration-keep repeating. for loop but the repetition has a order. Example: Family tree.)
- curry list. ->handle the remaining variable.
- lst (a:b:c:ds) =
- ^this pattern TELLS THAT IT IS A 100% LIST.
- Example: [2,3,4,5,6,7,8,9] a=2, b=3, c=4, ds=5~9
- the list provided minimum value must be 3! because got a,b,c
- ~~~~~~~~~~~~~~~~~~~~~~~~~~-}
- --Write a function that displays the first element in a list.
- --DONT USE HEAD.
- --myhead (a)=a
- myhead (a:b)=a
- --Write a function that displays the all elements in a list except the first element. DONT USE TAIL.
- mytail (a:b)=b
- {-~~~~~~~~~~~~~~~~~~~~~~~
- Example ::Factorial
- 5!=5x4!
- 4!=4x3!
- ...
- 1!=1
- n: 1x2x3x4...xn.
- Start/Stopping condition
- Recursive rule
- n!=nx(n-1)!
- 1!=1 (points to stop)
- ~~~~~~~~~~~~~~~~~~~~~~~~-}
- --Stopping criteria should always be at the top
- --SIMPLE RECURSION
- fact 1=1
- fact 0=1
- --generic pattern at below
- fact n=n*fact (n-1)
- --Fibonacci
- --MULTILEVEL RECURSION
- --stopping criteria
- fib 1=0
- fib 2=1
- --rule v
- fib n= fib (n-1)+fib(n-2)
- --write a function to add all numbers in a list
- --dont use sum function in your answer
- msum [] = 0
- msum (x:xs) = x + msum xs
- --write a function to display the last element in a list
- -- dont use last function in your answer
- mylast [] = 0
- mylast [x] = x
- mylast (x:xs) = mylast xs
- mylas (x:xs) = if(xs==[]) then x else mylas xs
- --rewrite the init function
- myinit [x] = []
- myinit (x:xs) = [x]++ myinit xs
- --disply the nth element in a list
- --disp 3[4,3,5,6,7,8,]=>5
- disp 1(x:xs) = x
- disp n(x:xs) = disp (n-1) xs
- --display the largest value in a list
- dislar [x] = x
- dislar (x:y:zs) = if(x>y) then dislar ([x]++zs) else dislar (y:zs)
- --reverse a list of values
- rev [] = []
- rev (a:as) = rev as++[a]
- --check if a list is a palidrome
- chkpalin xs = xs==rev(xs)
- --given a list, determine if it is sorted in the ascending order
- issorted [] = True
- issorted [x] = True
- issorted (x:y:zs) = if(x<y) then issorted (y:zs) else False
- myzip xs [] = []
- myzip [] ys = []
- myzip (x:xs)(y:ys) = [(x,y)]++ myzip xs ys
- {-~~~~~~~~~~~~~~Tutorial 29 November~~~~~~~~~~~~~~-}
- --Write a function to display the last element in a list.
- --dont use Last function
- mylast1 []=0
- mylast1 [x]=x
- mylast1 (x:xs) = mylast1 xs
- --Write a function to display the sum of the squares of numbers in a list.
- --Eg: mysum [1,4,2] = 21 (1^2+4^2+2^2=21) Dont use sum function
- mysum3 []=0
- mysum3 (x:xs)=x^2 + mysum3 xs
- --Write a function to display the smallest value in a list.
- --Eg: mysmall [1,0,1,0,2,1,3,0]=0
- mysmall [x]=x
- -- no nid this ->mysmall [x,y]=if(x<y) then x else y
- mysmall (x:y:xs)=if(x<y) then mysmall (x:xs) else mysmall (y:xs)
- --Write a function to display the smallest value in a list INCLUDING REPETITION.
- --Eg: allsmall [1,0,1,0,2,1,3,0]=[0,0,0]
- allsmall xs=[x|x<-xs,x==mysmall xs]
- {-~~~~~~~~~~~~~~~~Selection Sort~~~~~~~~~~~~~~~~~~-}
- --reuse allsmall and mysmall function
- notsmall xs=[x|x<-xs,x/=mysmall xs]
- selsort []=[]
- selsort xs=allsmall xs++selsort (notsmall xs)
- --Another answer
- selsort1 []=[]
- selsort1 xs=allsmall xs ++ selsort [x|x<-xs,x>mysmall xs]
- {-~~~~~~~~~~~~~~~~~Quick Sort~~~~~~~~~~~~~~~~~~~~~~-}
- --compare the x, check with xs. if smaller, put in [] front x, else larger, put into [] after x
- --eg:[5,3,7,2,6,0]
- quickSort []=[]
- quickSort [x]=[x]
- quickSort (x:xs)=quickSort [y|y<-xs,y<=x]++[x]++ quickSort [z|z<-xs,z>x]
- --Write a function to decompose a number into digits. Eg: decom 5368=[8,6,3,5]
- decom 0=[]
- decom x=decom (div x 10) ++[mod x 10]
- {-2% will come out in test-}
- {-~~~~~~~~~~~~~~~~~Merge Sort~~~~~~~~~~~~~~~~~~~~~~-}
- --Write a function 'merge', merging two SORTED LIST together.
- --example: [1,7,9,10] [2,4,5,6]
- merge [] x=x
- merge x [] =x
- merge (x:xs) (y:ys)= if(x<=y) then [x]++ merge xs (y:ys) else [y]++ merge (x:xs) ys
- --Mergesort
- msort [x]=[x]
- msort [x,y]=if (x<=y) then [x,y] else [y,x]
- msort xs=merge(msort(take (div (length xs) 2) xs)) (msort(drop (div (length xs) 2) xs))
- {-~~~~~~~~~~~~~~~~~~~~Lab Test covered in question!!~~~~~~~~~~~~~~~~~~~~~~~~~~~`-}
- --Decison Making
- -- if <exp> then <op1> else <op2>
- -- largest of 2 numbers
- --lar a b = if(a>b) then a
- --like this will have error, because then must has a "ELSE" ^
- lar1 a b = if(a>b) then a else b
- --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!
- --lar a b= if(a>b) then "first" else b
- lar a b= if(a>b) then "first" else "second"
- --Display the largest of three numbers
- largest a b c= if(a>b&&a>c) then show a ++ " is largest"
- else if(b>c) then show b++" is largest"
- else show c++" is largest"
- largest1 a b c= lar1 (lar1 a b) c
- --1. Read 3 numbers and determine if they are pythagorean triplets(a2 + b2 is equal = c2).
- pytha a b c= if((a^2)+(b^2)==c^2) then "Is pythagorean triplets" else "Not pythagorean triplets"
- --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
- absolute a=if(a<0) then (-a) else a
- --3. For given two numbers, determine if they divide each other perfectly(one way then is true).
- perfect a b=if((mod a b)==0) then "true" else if((mod b a)==0) then "true" else "false"
- perfect1 a b=or [mod a b==0, mod b a==0]
- --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)
- sinc a = if(a==0) then 1 else (sin a)/a
- --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.
- -------------------last---------middle-------------first num--
- armstrong1 a= if(mod a 10)^3+(mod(div a 10) 10)^3+(div a 100)^3==a then "true" else "false"
- --mod->get the remainder (divide by 10 will get the remainder)
- --div->get the answer (divide by 100 will get the 1st number)
- --Middle?divide by 10, get the 15, then mod 10 to get the remainder
- --_______________________________________________________________
- --Tutorial 3
- --_______________________________________________________________
- --Normal Function
- --funname a1 a2 a3 = exp
- {-Guarded Expressions(SwitchCase), if the condition is true
- funname a1 a2 a3 |<condition1> = <exp1>
- |<condition2> = <exp2>
- |<condition3> = <exp3>
- -}
- --Largest of two numbers (otherwise = Default)
- largest2 x y|(x>=y)=x
- |(y>=x)=y
- |otherwise=y
- --determine the smallest of 3 numbers
- smallest a b c|(a<b&&a<c)=a
- |(b<c)=b
- |otherwise=c
- {-
- *Pattern Matching / Polymorphism*
- 2 Categories: Adhoc(OOP-Java, C++, C#) & Parametric(Haskell, Python)
- -Adhoc-
- ~have more than 1 signature
- Essential to define polymorphism: same name for function, number of argument, different parameter and different datatype.
- ~Behave differently for same sum function.
- -Parametric-
- ~can have only 1 signature
- ~Cannot change the skeleton(the datatype returned, the datatype entered.)
- -define more than 1 version of the same function.
- Example:
- 1. myand True True = True ::Pattern matching can be perform. Variable data is given.
- 2. myand a b = False
- If called :"myand True False", will get 'false' because True False !=True True.
- Check the generic pattern after the SPECIFIC PATTERN.
- -}
- myand True True = True
- --the a b can only be Boolean, because the previous pattern is Boolean!!the skeleton already formed. :)
- myand a b =False
- --Implement sinc function using pattern matching
- --sinc of 0 is 1
- --sinc of x is (sin x)/x
- sinc1 0=1
- sinc1 x=(sin x)/x
- --The variable name of all type of data is the same.
- --Implement a function called safetail. This function returns an empty list if the input is an empty list.
- func1 a=a+1
- func2 a=length a
- func3 a=(a==True)
- safetail1 a=if (length a==0)then [] else tail a
- list []=[]
- --x is the only number in the list
- list [x]=[x]
- list [x,y]=[x,y]
- list a=tail a
- --Write a function that accepts a list of values. If the list has one or two or three elements do the following:
- --a
- --a+b
- --a-b+c
- --For more number of elements display their product.
- listFunc [x]=x
- listFunc [x,y]=x+y
- listFunc [x,y,z]=x-y+z
- --practice: as = give an infomation that it represents an List.
- listFunc as=product as
- --rewrite the product behavious such that the product of an empty list is 0.
- productEmpty []=0
- productEmpty as=product as
- --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.
- sorted [x] = [x]
- sorted [x,y]|(x>=y)=[y,x]
- |otherwise=[x,y]
- sorted [x,y,z]= if(y<z) then [y,z] else [z,y]
- sorted xs =[]
- 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]
- ------------------------------------------------------------------------------------------------
- /* John and Mary had two children named Alex and Susan.
- Alex married Diana. They had a child named Leo.
- Susan adopted two children named Philip and Daniel.
- Represent this information using suitable functors in your knowledge base.
- */
- father(john,alex).
- father(john,susan).
- mother(mary,alex).
- mother(mary,susan).
- father(alex,leo).
- mother(diana,leo).
- mother(susan,philip).
- mother(susan,daniel).
- /*
- Write rules to determine the following information.
- 1. Display the children given the name of father or mother.
- 2. Determine if two people may be siblings.(same parent)
- 3. Who are the parents of Daniel.
- 4. Who is the husband of Susan?
- */
- %Question 1
- par_chi(A,B):-mother(A,B);father(A,B).
- %Question 2
- isSibling(A,B):-par_chi(C,A),par_chi(C,B).
- %Question 3 just run by "par_chi(A,daniel).
- %Question 4-(write a generic rule.)
- couple(A,B):-par_chi(A,C),par_chi(B,C).
- %EXAM!!!Memorize the code
- % Recurrsion in Prolog.
- parent(john,alex).
- parent(alex,mary).
- parent(mary,peter).
- parent(peter,susan).
- %Stopping criteria.
- anc(A,B):-parent(A,B).
- %Recussion, if A not B's parent, find A's parent.
- anc(A,B):-parent(X,B),anc(A,X).
- %==================================%
- %No exam! List Comprehension. For knowledge.
- mylist(A,[A|T]).
- %mylist([sam, john, alex]).
- % A=sam.
- mylist(T,[A|T]).
- %mylist(A,[sam, john, alex]).
- % A=[john, alex].
- isfound(A,[A|T]).
- isfound(A,[H|T]):-isfound(A,T).
- friends([yemay,caifu,julius,alison,jay,samiha,yeesheng]).
- isfriend(A,B):-friends(X),isfound(A,X),isfound(B,X).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement