无法在我自己定义的数据结构上使用地图

时间:2018-11-06 18:12:12

标签: haskell

我定义了自己的玫瑰树,并尝试对其内容进行求和。所有类型都匹配,但是由于未知原因无法编译。
那是我的代码:

from tkinter import *
import tkinter as tk
import csv
import random
import sys

z = random.randint(1,50)
with open('vokabelnsemicolon.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')
    delist = []
    enlist = []
    for row in readCSV:
        eng = row[1]
        deu = row[0]

        delist.append(deu)
        enlist.append(eng)

LARGE_FONT= ("Verdana", 12)


class SeaofBTCapp(tk.Tk):


    def __init__(self, *args, **kwargs):


        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="German -> English",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = tk.Button(self, text="-> English -> German",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()



#German -> English
class PageOne(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="German -> English", font=LARGE_FONT)
        label.grid(row=0, column=0)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.grid(row=1, column=1)

        button2 = tk.Button(self, text="English -> German",
                            command=lambda: controller.show_frame(PageTwo))
        button2.grid(row=2, column=1)

        button3 = tk.Button(self, text="Check",
                            command=None)
        button3.grid(row=4, column=1)

        #text field for entry box
        Label(self, text='"'+enlist[z]+'"'+ ' in english is: ').grid(row=3, column=0)

        #entry field
        worden = Entry(self)
        worden.grid(row=3, column=1)


#English -> German
class PageTwo(tk.Frame):


    def checkybutton(self):
        print('checkbutton!')  

    def checkybutton2(self):
        print(controller.wordde.get())

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="English -> German", font=LARGE_FONT)
        label.grid(row=0, column=0)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.grid(row=1, column=1)

        button2 = tk.Button(self, text="German -> English",
                            command=lambda: controller.show_frame(PageOne))
        button2.grid(row=2, column=1)
        #text field for entry box
        Label(self, text='"'+delist[z]+'"'+ ' in german is: ').grid(row=3, column=0)


        button3 = tk.Button(self, text="checkbutton",
                            command=self.checkybutton)
        button3.grid(row=4, column=1)


        button4 = tk.Button(self, text="checkbutton 2",
                            command=self.checkybutton2)
        button4.grid(row=4, column=0)


        #entry field
        controller.wordde = Entry(self)

        wordde.grid(row=3, column=1)

        txt = Text(self, width=35, height=1, wrap=WORD)
        txt.grid(row=5, columnspan=2, sticky=W)    


app = SeaofBTCapp()
app.mainloop()

我遇到这些错误:

WHERE  EXISTS (SELECT A.MyField
               EXCEPT
               SELECT B.MyField) 

很清楚

data Tree a = Tree {element :: a, branch :: [Tree a]} deriving (Show)
sumTree :: (Num a) => Tree a -> a
sumTree x = element(x) + sum.map (sumTree) branch(x)

2 个答案:

答案 0 :(得分:4)

Haskell中的括号仅用于对代码元素进行分组。 Haskell函数调用语法不是f(x),而是f x

您写了

sumTree x = element(x) + sum.map (sumTree) branch(x)

相同
sumTree x = element x + sum . map sumTree branch x

但你的意思是

sumTree x = element x + sum . map sumTree (branch x)

该点仍然放错了位置,您希望将其放置

sumTree x = element x + (sum $ map sumTree (branch x))

相同
sumTree x = element x + sum (map sumTree (branch x))

答案 1 :(得分:2)

您在此处将map应用于branch :: Tree -> Tree a函数,而不是branch x的结果。这是因为括号写错了。

您可以将功能实现为:

sumTree :: Num a => Tree a -> a
sumTree x = element x + sum (map sumTree (branch x))

因此,我们在此使用mapsumTree作为参数调用(branch x)

此处branch x将生成分支列表,然后在该列表上调用map sumTree以生成每个子树的总和,然后将它们与sum进行汇总。然后,我们将element x添加到结果中。