Apart from creating functions that do simple things to lists, I'm pretty new to haskell. I would like to create a list which contains things of type Int, and functions of type Int -> Int -> Int.
Here is what I have tried:
data Token = Value Int | Operator (Int -> Int -> Int)
tokens :: [Token]
tokens = [12, (+)]
but I get the following error
Couldn't match expected type `Token'
with actual type `Integer -> Integer -> Integer'
In the expression: (+)
In the expression: [12, (+)]
In an equation for `tokens': tokens = [12, (+)]
I'm not sure why this doesn't work, can anyone point me in the right direction?
The int() function converts the specified value into an integer number.
It is a so called "type hint" (or "function annotation"; these are available since Python 3.0). -> List[int] means that the function should return a list of integers.
In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.).
You need to use your constructors to obtain values of type Token. For example, 12 is not of type Token, it is of type Int (well, Num a => a). Similarly, (+) is not a token but a function Int -> Int -> Int. Notice that Token /= Int -> Int -> Int.
Fortunately you have defined a few constructors such as Value :: Int -> Token and Operator :: (Int -> Int -> Int) -> Token. So using those we get:
tokens :: [Token]
tokens = [Value 12, Operator (+)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With