Search code examples
haskellrecursiongrammaralgebraic-data-typesrecursive-datastructures

Haskell Data Types


Which is not value of the Bin data types as defined below?

data Bin = B Bin | C [ Int]

a) C [ ]

b) B ( B C [ 2])

c) B ( C [ 1..5])

d) B (B (B (C[2,3])))


Solution

  • By using ghci and simply inputing the code snippets you will see which of the values are accepted:

    Prelude> data Bin = B Bin | C [ Int]
    Prelude> a = C [ ]
    Prelude> b = B ( B C [ 2])
    
    <interactive>:3:9: error:
        • Couldn't match expected type ‘[Integer] -> Bin’
                      with actual type ‘Bin’
        • The function ‘B’ is applied to two arguments,
          but its type ‘Bin -> Bin’ has only one
          In the first argument of ‘B’, namely ‘(B C [2])’
          In the expression: B (B C [2])
    
    <interactive>:3:11: error:
        • Couldn't match expected type ‘Bin’
                      with actual type ‘[Int] -> Bin’
        • Probable cause: ‘C’ is applied to too few arguments
          In the first argument of ‘B’, namely ‘C’
          In the first argument of ‘B’, namely ‘(B C [2])’
          In the expression: B (B C [2])
    Prelude> c = B ( C [ 1..5])
    Prelude> d = B (B (B (C[2,3])))