Question: In Haskell, I want to write a function( squareNeg2 ) which takes a list and returns a list that if there is a negative value
In Haskell,
I want to write a function(squareNeg2) which takes a list and returns a list that if there is a negative value in the list, then squares the value.
The meaning of squareNeg1 and squareNeg2 is same, but squareNeg2 should use 'map' and 'either' function.
squareNeg1 :: (Ord a, Num a) => [a] -> [a] squareNeg1 [] = [] squareNeg1 (x:xs) | x <= 0 = x^2 : squareNeg1 xs | otherwise = x : squareNeg1 xs
ex. squareNeg1 [2,3,-1,-3,-5,4] [2,3,1,9,25,4]
=========================
squareNeg2 :: (Ord a, Num a) => [a] -> [a] squareNeg2 [] = [] squareNeg2 (x:xs) =
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
