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


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值