是否可以從 Main.hs 參考我的 Graph.hs -functions、edgs 和 vers in data Graphs a?
我想從我的 Graphs.hs 檔案創建一個 Graph,用頂點填充它,并用邊連接它們。然后從我的 Main.hs 檔案中,我想創建一個函式 getEdges,它可以為我列出所有邊。這是可能的還是我以 OOP 的方式想得太多?:-(
下面我添加了部分代碼來嘗試說明我的問題。
**File Graph.hs:**
module Graph(Graph, addVertex, addEdge, connectedVertex) where
type Vertex a = a
type Edge a = (Vertex a, Vertex a)
data Graph a = Graph {
vers :: [Vertex a],
edgs :: [Edge a]
} deriving (Show, Eq, Read)
**File Main.hs:**
-- func1 to func3 is just to illustrate that there is more than one function in main.
module Main(func1, func2, getEdges, paths) where
import Graph
-- getEdges shall return a list of all edges
getEdges :: Eq a => Grap a -> [a]
getEdges g = edgs $ g
-- paths will return true if there is a connection (edges) between vertecies a to b.
paths :: Eq a => Graph a -> a -> a-> Bool
paths = undefined
我將不勝感激任何幫助:-)
uj5u.com熱心網友回復:
你缺少一個(..)在module Graph(Graph(..), addVertex, addEdge, connectedVertex) where
一個較小的同樣說明性的例子:
模塊.hs:
module Module(Record(..)) where
data Record a = Record {
string :: String,
int :: Int
} deriving (Show, Eq, Read)
和 Main.hs:
module Main where
import Module
record = Record "Hello" 42
main =
print (string record) >>
print (int record)
該Type(..)出口型構造及其相關資料的構造和現場訪問器。您還可以在上面的示例module Module(Record(Record), int, string) where中對需要匯出的內容進行更細粒度的控制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/386986.html
標籤:哈斯克尔
