snikers12
snikers12

Reputation: 13

Haskell: Function as Typedeclaration

I want to implement some functions for a graph.

The type declaration is:

type Graph = Knoten -> [Knoten]

For one function I have to go through the list [Knoten]. I have the right algorithm but don't know how to access [Knoten].

Here are my attempts:

ist_minimal :: Graph -> Bool
ist_minimal g = minimalHelp g

minimalHelp :: Knoten -> [Knoten] -> Bool
minimalHelp _ [] = True
minimalHelp k (m:ms) 
 | elem m ms = False
 | otherwise = minimalHelp k ms

ist_minimal :: Graph -> Bool
ist_minimal (k ks) = minimalHelp (k ks)     --k=Knoten, ks=[Knoten]

minimalHelp :: Knoten -> [Knoten] -> Bool
minimalHelp _ [] = True
minimalHelp k (m:ms) 
 | elem m ms = False
 | otherwise = minimalHelp k ms

My consideration was, that when

Graph = Knoten -> [Knoten]

it is possible to write Graph as Knoten [Knoten]

Upvotes: 1

Views: 53

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170713

That won't work. Knoten -> [Knoten] -> Bool means Knoten -> ([Knoten] -> Bool) while Graph -> Bool means (Knoten -> [Knoten]) -> Bool and those are very different types.

Basically the only thing you can do with a Knoten -> [Knoten] (and so with a Graph) is to invoke it on some Knoten, so your function will need to be built from this operation. E.g. one possible ist_minimal :: Graph -> Knoten which calls minimal_help would be

ist_minimal g = minimal_help k (g k)
    where k = ...

and ... will need to be filled to give you something of type Knoten: g k will have type [Knoten] and so this code passes a Knoten and a [Knoten] to minimal_help, as its type requires.

Upvotes: 1

Related Questions