Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Even Function?

Tags:

haskell

This is probably a fairly obvious question, but I just can't figure it out.

I am trying to write a function that squares the even numbers in a list. When I try to run it, I am getting an error about my use of the even function. How can I fix this?

module SquareEvens where

squareEvens :: [Integer] -> [Integer]

squareEvens n = [ns * ns | ns <- n, even n]
like image 910
Justin Tyler Avatar asked Dec 02 '22 19:12

Justin Tyler


1 Answers

The code works fine if you change even n to even ns:

squareEvens n = [ns * ns | ns <- n, even ns]

But note that the convention is to use the plural to name the list and the singular to name an element from that list. So swap n and ns to follow idiomatic Haskell usage:

squareEvens ns = [n * n | n <- ns, even n]
like image 70
dave4420 Avatar answered Dec 04 '22 09:12

dave4420