Haskell学习2

Problem 1:Implement a function isIn in Haskell which takes an element x and a list xs as arguments and returns True i x is a member of list xs.

--Problem 1:isIn
--Name:Wenqi Sun
--Student ID:2010202521

--isIn
--compare the number one by one
isIn p q =if q==[]
            then False
            else if  p==head(q)
                 then True
                 else isIn p (tail(q))


Problem 2:Implement a function remDouble which takes a list as an argument and returns the list with all duplicates removed.

--Problem 2:remDouble
--Name:Wenqi Sun
--Student ID:2010202521


--No duplicate insert
insert x []=[x]              
insert x (y:xs)
  | x<y =x:y:xs
  | x>y=y:(insert x xs)
  | x==y =(insert x xs)      --ensure no duplicates

--remDouble
remDouble  xs=sortIns(xs,[])            --InsertSort
              where
                sortIns([],ys)=ys
                sortIns (x:xs,ys)=sortIns(xs,(insert x ys))                  


Problem 3:A set may be represented as a list of elements without duplicates; the powerset of a set is the set of all subsets of that set.Implement a function powerSet such that powerset xs returns all subsets of xs, when xs and the subsets are represented as lists:
Example: subsets [1, 2, 3] should return [[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]] in this or in some other order.
Hint: If x∉2 A then P(A∪{x}) = P(A)∪{{x}∪B|B∈P(A)} with P(A) as the power set of set A.

--Problem 3:powerSet
--Name:Wenqi Sun
--Student ID:2010202521


--add
add x []=[]
add x (y:ys)=(x:y):(add x ys)       --add the new number to the element of  set 

--powerSet
powerSet q=power q [[]]
       where
         power [] ys=ys
         power  (x:xs) ys=(power xs ys)++(power xs (add x ys))         --as problem 3 hint describes


你可能感兴趣的:(Haskell学习2)