Checking square root in chapter 4

As I understand the exercise, I have to write a function that takes in an array and sqare roots each number in it.

I have this solution now

squared :: Array Number -> Array Number
squared arr = (\n -> sqrt(n)) <$> arr

However, it gives me this error output

[info] Build succeeded.
→ Suite: Chapter Examples
  ✓ Passed: fact
  ✓ Passed: fib
  ✓ Passed: length
  ✓ Passed: factors
  ✓ Passed: factorsV2
  ✓ Passed: factorsV3
  ✓ Passed: factTailRec
  ✓ Passed: lengthTailRec
  ✓ Passed: allFiles
  ✓ Passed: allFiles'
→ Suite: Exercise Group - Recursion
  ✓ Passed: Exercise - isEven
  → Suite: Exercise - countEven
    ✓ Passed: [] has none
    ✓ Passed: [0] has 1
    ✓ Passed: [1] has 0
    ✓ Passed: [0, 1, 19, 20] has 2
→ Suite: Exercise Group - Maps, Infix Operators, and Filtering
  → Suite: Exercise - squared
    ✓ Passed: Do nothing with empty array
    ☠ Failed: Calculate squares because expected [0.0,1.0,4.0,9.0,10000.0], got [0.0,1.0,1.4142135623730951,1.7320508075688772,10.0]

1 test failed:

In "Exercise Group - Maps, Infix Operators, and Filtering":
  In "Exercise - squared":
    In "Calculate squares":
      expected [0.0,1.0,4.0,9.0,10000.0], got [0.0,1.0,1.4142135623730951,1.7320508075688772,10.0]

[error] Tests failed: exit code: 1

Did I misunderstand the exercise or should I miss a step there? It doesn’t give an error for the function itself

Here’s the exercise for the others:

(Easy) Write a function squared which calculates the squares of an array of numbers. Hint: Use the map or <$> function.

@nospace The exercise states that squared should square the numbers in the array, not get their square roots like you thought.

1 Like

So it means the opposite of what I tried to do, thanks.

This is the function I have now, written in power to two, by changing to

import Math (pow)
[...]
squared :: Array Number -> Array Number
squared arr = (\n -> pow n 2 ) <$> arr

but it gives me a different error output

Error found:
in module Test.MySolutions
at test/MySolutions.purs:42:29 - 42:30 (line 42, column 29 - line 42, column 30)

  Could not match type
       
    Int
       
  with type
          
    Number
          

while checking that type Int
  is at least as general as type Number
while checking that expression 2
  has type Number
in value declaration squared

See https://github.com/purescript/documentation/blob/master/errors/TypesDoNotUnify.md for more information,
or to contribute content related to this error.


[error] Failed to build.

In PureScript, all number literals that don’t have a decimal point are considered integers. You provide pow, which expects both of its parameters to be Numbers, with an Int (2). So, instead of pow n 2, you should use pow n 2.0.

Personally, I don’t see the need to use pow here and would instead just use n * n. However, pow also works, and you can use that if you prefer.

2 Likes

It worked either way, thank you

1 Like