Program that returns the sum of squares of only the positive numbers in a list

Ohad

I tried to make a program in Haskell that returns the sum of squares of only the positive numbers in a list by using the filter function.

this is my first attempt:

sumsq :: [Int] -> Int
sumsq xs = foldr (+) 0 filter isPositive xs

isPositive :: Int -> Bool
isPositive x | x > 0     = True
             | otherwise = False

but it's not working and this is the error message I am getting :

Couldn't match expected type `[(Int -> Bool) -> [Int] -> Int]'
            with actual type `(a0 -> Bool) -> [a0] -> [a0]'
In the third argument of `foldr', namely `filter'
In the expression: foldr (+) 0 filter isPositive xs
In an equation for ` sumsq':
     sumsq xs = foldr (+) 0 filter isPositive xs

solution: after adding parentheses it's working correctly.

    sumsq :: [Int] -> Int
    sumsq xs = foldr (+) 0 (filter isPositive xs)

    isPositive :: Int -> Bool
    isPositive x | x > 0 = True

my new question is: why is it working with the parentheses and not without the parentheses ?

Wyzard

Without the parentheses, you're saying that filter, isPositive, and xs are all arguments to the fold. That doesn't work because foldr expects a list as its third argument, not two functions followed by a list. What you want is to pass the result of calling filter isPositive xs as the third argument to foldr, and that's what the parentheses specify.

You can get the same effect by writing foldr (+) 0 $ filter isPositive xs, since the $ operator basically means to wrap a set of parentheses around everything that follows it.

Note that you can also write this function more concisely in point-free form (no need to mention xs) and without an explicit fold, using function composition:

sumsq = sum . map (^2) . filter (>0)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cumulative sum for positive numbers only

From Dev

Sum the squares of the even numbers from a list in Haskell

From Dev

Compute the sum of the positive numbers in a list in Python

From Dev

Write a program to find sum of squares of all numbers given the following conditions?

From Dev

pandas dataframe groupby: sum/count of only positive numbers

From Dev

Creating a predicate in Prolog that sums the squares of only the even numbers in a list

From Dev

Decimal negative and positive numbers sum

From Dev

sum of squares in a list in one line?

From Dev

How do I filter and sum only negative or positive numbers in a field using an expression in SQL Reporting Services 2014

From Dev

Sum odd numbers program

From Dev

Sum of positive elements in linked list

From Dev

Prolog program - list of Peano numbers - rule returns multiple answers

From Dev

Finding number of positive numbers in list

From Dev

Return positive numbers of a list in python

From Dev

Finding number of positive numbers in list

From Dev

How to write a function that takes a positive integer N and returns a list of the first N natural numbers

From Dev

Recursively finding a sum of perfect squares in a list

From Dev

Scheme: Program to iterate through a list and return only the non-numbers

From Dev

Python: how to get only positive numbers of nested int list with resonable performance

From Dev

Sum only positive elements of a vector CUDA/THRUST

From Java

How to make type="number" to positive numbers only

From Dev

Summing only positive numbers in PHP array

From Dev

Check if string contains only positive numbers in Ruby

From Dev

How to only allow positive numbers in an EditText

From Dev

How to add only positive numbers in shell script?

From Dev

R: Summary information only for positive numbers in a range

From Dev

Program in Prolog for sum of numbers in interval

From Dev

Program in Prolog for sum of numbers in interval

From Dev

Write a program to sum 2 numbers

Related Related

  1. 1

    Cumulative sum for positive numbers only

  2. 2

    Sum the squares of the even numbers from a list in Haskell

  3. 3

    Compute the sum of the positive numbers in a list in Python

  4. 4

    Write a program to find sum of squares of all numbers given the following conditions?

  5. 5

    pandas dataframe groupby: sum/count of only positive numbers

  6. 6

    Creating a predicate in Prolog that sums the squares of only the even numbers in a list

  7. 7

    Decimal negative and positive numbers sum

  8. 8

    sum of squares in a list in one line?

  9. 9

    How do I filter and sum only negative or positive numbers in a field using an expression in SQL Reporting Services 2014

  10. 10

    Sum odd numbers program

  11. 11

    Sum of positive elements in linked list

  12. 12

    Prolog program - list of Peano numbers - rule returns multiple answers

  13. 13

    Finding number of positive numbers in list

  14. 14

    Return positive numbers of a list in python

  15. 15

    Finding number of positive numbers in list

  16. 16

    How to write a function that takes a positive integer N and returns a list of the first N natural numbers

  17. 17

    Recursively finding a sum of perfect squares in a list

  18. 18

    Scheme: Program to iterate through a list and return only the non-numbers

  19. 19

    Python: how to get only positive numbers of nested int list with resonable performance

  20. 20

    Sum only positive elements of a vector CUDA/THRUST

  21. 21

    How to make type="number" to positive numbers only

  22. 22

    Summing only positive numbers in PHP array

  23. 23

    Check if string contains only positive numbers in Ruby

  24. 24

    How to only allow positive numbers in an EditText

  25. 25

    How to add only positive numbers in shell script?

  26. 26

    R: Summary information only for positive numbers in a range

  27. 27

    Program in Prolog for sum of numbers in interval

  28. 28

    Program in Prolog for sum of numbers in interval

  29. 29

    Write a program to sum 2 numbers

HotTag

Archive