Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a list of Ints and functions Int -> Int -> Int

Tags:

haskell

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?

like image 506
Cameron Martin Avatar asked Aug 07 '14 04:08

Cameron Martin


People also ask

What does -> int do in Python?

The int() function converts the specified value into an integer number.

What does -> list mean in Python?

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.

How do you make a list of integers in Python?

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.).


1 Answers

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 (+)]
like image 118
Thomas M. DuBuisson Avatar answered Oct 13 '22 07:10

Thomas M. DuBuisson