elm/core

Basics docs TYPE MISMATCH: sqrt >> isEven >> not

geofflangenderfer opened this issue · 1 comments

This shows up in a comment and in the docs.

A compilable example would save beginners' time. For example:

> composed = cos >> sin
<function> : Float -> Float
> composed pi
-0.8414709848078965 : Float
> sin (cos pi)
-0.8414709848078965 : Float
> sin (cos pi) == composed pi
True : Bool

I believe it's an error:

>import Arithmetic
> sqrt
<function> : Float -> Float
> Arithmetic.isEven
<function> : Int -> Bool
> not
<function> : Bool -> Bool
> sqrt >> Arithmetic.isEven >> not
-- TYPE MISMATCH ---------------------------------------------------------- REPL

The right argument of (>>) is causing problems.

4|   sqrt >> Arithmetic.isEven >> not
             ^^^^^^^^^^^^^^^^^^^^^^^^
The right argument is:

    Int -> Bool

But (>>) needs the right argument to be:

    Float -> c

Hint: With operators like (>>) I always check the left side first. If it seems
fine, I assume it is correct and check the right side. So the problem may be in
how the left and right arguments interact!

Note: Read <https://elm-lang.org/0.19.1/implicit-casts> to learn why Elm does
not implicitly convert Ints to Floats. Use toFloat and round to do explicit
conversions.

Here's a correction:

> checkSqrtOdd x = sqrt x - (x |> sqrt |> floor |> toFloat) > 0.001
> checkSqrtOdd 15
True : Bool
> checkSqrtOdd 16
False : Bool
> checkSqrtOdd 17
True : Bool

Thanks for reporting this! To set expectations:

  • Issues are reviewed in batches, so it can take some time to get a response.
  • Ask questions a community forum. You will get an answer quicker that way!
  • If you experience something similar, open a new issue. We like duplicates.

Finally, please be patient with the core team. They are trying their best with limited resources.