Haskell type-mapping (-> operator) in where where?

I am trying to understand the code in this article . This explains the use of inductive graphs, which seems very nice, and at some point it determines the depth search for inductive graphs. The code for it is as follows:

dfs :: Graph.Node -> Gr ab -> [Node] dfs start graph = go [start] graph where go [] _ = [] go _ g | Graph.isEmpty g = [] go (n:ns) (match n -> (Just c, g)) = n : go (Graph.neighbors' c ++ ns) g go (_:ns) 

I do not understand these two lines:

  go (n:ns) (match n -> (Just c, g)) = n : go (Graph.neighbors' c ++ ns) g 

It seems to define a go function that takes a list as the first argument, which maps to the pattern (n:ns) . The second argument, however, I do not understand: (match n -> (Just c, g)) . What does the -> operator mean here? Looking up an operator can be one of three things:

  • Function type mapping operator.
  • The lambda definition operator.
  • Separator when building the hull.

Since there is no case or backslash variable for a lambda expression, this can only happen if it is a function type mapping operator. In this case, I donโ€™t understand how it binds the values โ€‹โ€‹to these variables, c and g ? And what does this exactly mean, how can it be in an argument?

Thanks in advance!

+5
source share
2 answers

-> means not the type of function, nor the lambda definition, nor the display of cases in this case. This is a view template .

 go (n:ns) (match n -> (Just c, g)) = n : go (Graph.neighbors' c ++ ns) g 

equivalently

 go (n:ns) g' | (Just c, g) <- match ng' = n : go (Graph.neighbors' c ++ ns) g 

where the template keeper (Just c, g) <- match n g' , in turn, is reduced to

 go (n:ns) g' = case match ng' of (Just c, g) -> n : go (Graph.neighbors' c ++ ns) g (Nothing, g) -> ... 

where the Nothing clause should be behind the later clause of the definition of go .

+8
source

This is a view template . This extension allows us to somehow transform the argument before matching it.

Without this extension, the code should be written as follows:

 go (n:ns) g' = case match ng' of (Just c, g) -> ... ... 

which is a bit more verbose.

+3
source

Source: https://habr.com/ru/post/1241411/


All Articles