1

I got 2 types

type Rotor = String 
type Reflector =[(Char, Char)]

and I know type SimpleEnigma has exactly 3 Rotor and 1 Reflector. How can I define SimpleEnigma?

Thanks in advance!!

1 Answer 1

2
data SimpleEnigma = SimpleEnigma Rotor Rotor Rotor Reflector

We use the data keyword to define a new algebraic data type that holds three values of type Rotor and one value of type Reflector.

Note that you change the name of the value constructor (SimpleEnigma to the right of the equal) to something other than SimpleEnigma and change the order of types Rotor and Reflector.

5
  • Why do we need the SimpleEnigma to the right of the equal and if I use this type, what does it return?
    – user6929012
    Commented Nov 7, 2016 at 0:54
  • 2
    SimpleEnigma to the left is a type. SimpleEnigma to the right is a value constructor: learnyouahaskell.com/… Commented Nov 7, 2016 at 0:58
  • The type keyword introduces a type alias, whereas the data constructor introduces new ADTs.
    – mnoronha
    Commented Nov 7, 2016 at 1:00
  • wiki.haskell.org/Keywords#data and wiki.haskell.org/Keywords#type should be helpful.
    – mnoronha
    Commented Nov 7, 2016 at 1:00
  • @mnoronha You know the Maybe type? It's defined as data Maybe t = Just t | Nothing i.e. Maybe t is the type itself (on the left) and Just t and Nothing define the possible values of that type. Commented Nov 7, 2016 at 2:08