icyrock.com

Home

PureScript solutions to Project Euler - problem 1

2017-Oct-31 00:18
purescriptproject-euler

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Euler001Test (euler1suite) where
 
import Prelude
 
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
import Euler001 (euler1)
 
euler1suite :: forall e. TestSuite e
euler1suite =
  suite "Euler 1" do
    test "Warmup" do
      Assert.equal 23 (euler1 10)
    test "Real" do
      Assert.equal 233168 (euler1 1000)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
module Euler001 (euler1) where
 
import Prelude
 
import Data.Array (filter, range)
import Data.Foldable (sum)
 
euler1 :: Int -> Int
euler1 n =
  range 1 (n - 1)
  # filter (\e -> e `mod` 3 == 0 || e `mod` 5 == 0)
  # sum

Comments

Purescript uses test/Main.purs as the main test file. To split suites for different problems this can be used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Test.Main where
 
import Prelude
 
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.AVar (AVAR)
import Control.Monad.Eff.Console (CONSOLE)
import Euler001Test (euler1suite)
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)
 
main :: forall e.
  Eff
    ( console :: CONSOLE
    , testOutput :: TESTOUTPUT
    , avar :: AVAR
    | e
    )
    Unit
main = runTest do
  euler1suite
Additional suites would then be added as problems are solved.

Running all the suites is done with the usual purs test.