Haskell Strange Operator

For assignment for my functional programming class, I am working on an exercise on trees. And, more specifically, on pink trees. Within the framework that we received, the data type "Rose" is already defined, but it has an operator:>. I searched him on hoogle and he said that this is the rightmost element of the sequence and the rest of the sequence. (Part of the frame below)

data Rose a = a :> [Rose a] deriving (Eq, Show) -- Exercise 1 root :: Rose a -> a root = undefined children :: Rose a -> [Rose a] children = undefined 

By no means do I want you guys to tell me how the root and children function should be executed. But if you guys can give me some tips on how to read the line β€œRose a data” or maybe show me how the rosewood will grow. These things would be really great help since I enjoyed doing the exercises.

Hope someone can point me in the right direction.

+5
source share
3 answers

t is very similar to a list definition:

  • (:>) bit like (:)
  • data Rose a = a :> [Rose a] tells you that you can get t :: Rose a
    • with element x :: a
    • and children rs :: [Rose a]
    • t = x :> rs

Of course, you can also return such elements:

 root (x :> rs) = ... 

I hope you leave everything yourself;)

+9
source

The operator :> actually a data constructor. It would be equivalent to define the type as

 data Rose a = Node a [Rose a] 

Where (:>) = Node . So with this alternative definition you would have

 root :: Rose a -> a root (Node a subnodes) = a 

Substituting for the actual constructor :> you would have

 root ((:>) a subnodes) = a 

What can also be written as

 root (a :> subnodes) = a 

Like @Carsten says he is as a list constructor : just specialized for the Rose data type.

+9
source

The operator :> does not have a predefined value. This is introduced by your definition of Rose, as is the identifier of Rose . In Haskell, you can define your own operators, this is one of them.

This particular operator is a data constructor that can be defined by the first character. Data constructors begin with a colon. Operators that do not start with a colon are normal functions.

It works just like any other data constructor, the only difference is that it is written in infix form, for example : In this program, it can be considered "attached to."

Since everyone can use it in their own program for any purpose, this is pointless for google.

+6
source

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


All Articles