我是Haskell的初学者。我在书中发现了一个问题:检查列表是否具有“山”的方面,升序元素 - 降序元素。检查列表是否具有“谷”的方面,降序元素 - 升序元素。请帮助我。
例:
“山”:[1,2,3,4,5,6,3,2,1]
“山谷”:[4,3,2,1,1,2,3,4]
我的代码看起来:
山
ordered::[Int]->Bool
ordered [a]=True
ordered (a:l)=if(a>head(l)) then False
else ordered(l)
谷
ordered::[Int]->Bool
ordered [a]=True
ordered (a:l)=if(a<head(l)) then False
else ordered(l)
答案 0 :(得分:2)
您应该首先考虑基本情况。您已经开始使用包含1个元素的列表的基本案例,但是有一个更简单的基本案例:空列表。空列表通常被视为 升序和降序。接下来你要做的就是退出head
。它是偏袒的,会引导你犯错误。相反,模式匹配所有内容。
在编写一个非常简单的版本之后,在编写模式匹配版本之后提示:使用zipWith
和drop
。请注意,与tail
不同,drop
是一个总函数。
答案 1 :(得分:1)
localMinima :: [Integer] -> Integer
localMinima [x]=x
localMinima (x:xs) = if x<m then x
else m where m=localMinima xs
rmAdjacentDups :: [Integer]->[Integer]
rmAdjacentDups [] = []
rmAdjacentDups (x:xs) = x : (rmAdjacentDups $ dropWhile (== x) xs)
isValley :: [Integer] -> Bool
isValley xs = 1 == (length $ localMinima $ rmAdjacentDups xs)
答案 2 :(得分:0)
这是一个策略:如果删除相邻的重复元素后,列表只有一个local maximum,则列表为“山峰”。同样,“谷”是一旦删除相邻重复元素就只有一个局部最小值的列表。下面的函数将告诉列表是山还是山谷:
from tkinter import *
myGui=Tk()
myGui.geometry("800x600")
myGui.title("Pete's Pizza Parlour~Order Form")
#TOPPING SELECTION
toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue").pack()
a=IntVar()
olives_chk=Checkbutton(myGui,text="Olives",variable=a).pack()
b=IntVar()
tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b).pack()
c=IntVar()
pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c).pack()
d=IntVar()
hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d).pack()
e=IntVar()
onions_chk=Checkbutton(myGui,text="Onions",variable=e).pack()
f=IntVar()
ham_chk=Checkbutton(myGui,text="Ham",variable=f).pack()
g=IntVar()
sausage_chk=Checkbutton(myGui,text="Sausage",variable=g).pack()
h=IntVar()
greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h).pack()
olivesSelectionStr="olives"
tomatoesSelectionStr="tomatoes"
pepperoniSelectionStr="pepperoni"
hotPeppersSelectionStr="hot peppers"
onionsSelectionStr="onions"
hamSelectionStr="ham"
sausageSelectionStr="sausage"
greenPeppersSelectionStr="green peppers"
noToppingsStr="no toppings."
def checkToppings():
toppingsList=""
olivesSelection=a.get()
tomatoesSelection=b.get()
pepperoniSelection=c.get()
hotPeppersSelection=d.get()
onionsSelection=e.get()
hamSelection=f.get()
sausageSelection=g.get()
greenPeppersSelection=h.get()
if(olivesSelection==1):
toppingsList=toppingsList+olivesSelectionStr
if(tomatoesSelection==1):
toppingsList=toppingsList+tomatoesSelectionStr
if(pepperoniSelection==1):
toppingsList=toppingsList+pepperoniSelectionStr
if(hotPeppersSelection==1):
toppingsList=toppingsList+hotPeppersSelectionStr
if(onionsSelection==1):
toppingsList=toppingsList+onionsSelectionStr
if(hamSelection==1):
toppingsList=toppingsList+hamSelectionStr
if(sausageSelection==1):
toppingsList=toppingsList+sausageSelectionStr
if(greenPeppersSelection==1):
toppingsList=toppingsList+greenPeppersSelectionStr
if toppingsList=="":
toppingsList=noToppingsStr
print(toppingsList)
topping_btn = Button(myGui,text='print toppings', command = checkToppings)
topping_btn.pack()
myGui.mainloop()
如果您实施isMountain :: [Integer] -> Bool
isMountain xs = 1 == (length $ localMaxima $ rmAdjacentDups xs)
isValley :: [Integer] -> Bool
isValley xs = 1 == (length $ localMinima $ rmAdjacentDups xs)
*Main> isMountain [1,2,3,4,5,6,3,2,1]
True
*Main> isMountain [1,2,3,4,5,6,5,6,3,2,1]
False
*Main> isValley [4,3,2,1,1,2,3,4]
True
*Main> isValley [4,3,2,1,2,1,1,2,3,4]
False
,localMaxima
或localMinima
,请发表评论。希望这有帮助!