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

StackOverflow https://stackoverflow.com/questions/18611039

  •  27-06-2022
  •  | 
  •  

Question

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?

Était-ce utile?

La solution

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]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top