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

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

  •  27-06-2022
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top