[x | x <- [1..999] , x `mod` 3 == 0 || x `mod` 5 == 0] C int main(void) { int total=0; int i=0; while(i<1000) { if(i%3==0 || i%5==0) total+=i; i++; } printf("%d", total); } Python def multiples_of_3_or_5(): for number in xrange(1000): if not number % 3 or not number % 5: yield number print sum(multiples_of_3_or_5()) Or, you can use list comprehension print sum( number for number in xrange(1000) if not (number % 3 and number % 5) )
:: Num a => a Prelude> :t 0.5 0.5 :: Fractional a => a Why? 1 can be used with any numeric type; 0.5 can be used with any numeric type that supports fractions
a typeclass, not a type. A typeclass tells you about a type. Num a tells you that the type a must support numeric operations like +, *, and - value type context
"square is a function that takes an a and returns an a, where a is a numeric type." Note: the input and output type must match: square cannot return a Float if you give it an Int. value input output context type
information about something in GHCi. For a typeclass, :i will give you its operations and a list of types that are its instances. class Num a where (+) :: a -> a -> a (*) :: a -> a -> a (-) :: a -> a -> a negate :: a -> a abs :: a -> a signum :: a -> a fromInteger :: Integer -> a -- Defined in `GHC.Num' instance Num Integer -- Defined in `GHC.Num' instance Num Int -- Defined in `GHC.Num' instance Num Float -- Defined in `GHC.Float' instance Num Double -- Defined in `GHC.Float'