How do I make the power function, doubles and [..] working together in frege?

72 views Asked by At

While

map (\x -> x * x) [0..9]

is working fine (also list comprehension), I cannot do

map (** 2) [0..9]

since the power operator requires doubles and the .. operator does not allow them.

Is there some mapping that I can use?

1

There are 1 answers

0
Ingo On BEST ANSWER

The reason is that Double is not an instance of Enum.

There are 2 possibilities:

  1. Make Double an instance of Enum.
  2. Use the function that converts Int values to any numeric type you need:

For example:

(map (** 2) . map fromInt) [0..9]

or, if you prefer:

map ((** 2) . fromInt) [0..9]