Skip to content

Commit 9219f0f

Browse files
committed
Add examples
1 parent 8022beb commit 9219f0f

File tree

9 files changed

+229
-8
lines changed

9 files changed

+229
-8
lines changed

difference-of-squares/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Difference Of Squares
2+
3+
Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
4+
5+
The square of the sum of the first ten natural numbers is
6+
(1 + 2 + ... + 10)² = 55² = 3025.
7+
8+
The sum of the squares of the first ten natural numbers is
9+
1² + 2² + ... + 10² = 385.
10+
11+
Hence the difference between the square of the sum of the first
12+
ten natural numbers and the sum of the squares of the first ten
13+
natural numbers is 3025 - 385 = 2640.
14+
15+
You are not expected to discover an efficient solution to this yourself from
16+
first principles; research is allowed, indeed, encouraged. Finding the best
17+
algorithm for the problem is a key skill in software engineering.
18+
19+
20+
## Getting Started
21+
22+
Please refer to the [installation](https://exercism.io/tracks/haskell/installation)
23+
and [learning](https://exercism.io/tracks/haskell/learning) help pages.
24+
25+
## Running the tests
26+
27+
To run the test suite, execute the following command:
28+
29+
```bash
30+
stack test
31+
```
32+
33+
#### If you get an error message like this...
34+
35+
```
36+
No .cabal file found in directory
37+
```
38+
39+
You are probably running an old stack version and need
40+
to upgrade it.
41+
42+
#### Otherwise, if you get an error message like this...
43+
44+
```
45+
No compiler found, expected minor version match with...
46+
Try running "stack setup" to install the correct GHC...
47+
```
48+
49+
Just do as it says and it will download and install
50+
the correct compiler version:
51+
52+
```bash
53+
stack setup
54+
```
55+
56+
## Running *GHCi*
57+
58+
If you want to play with your solution in GHCi, just run the command:
59+
60+
```bash
61+
stack ghci
62+
```
63+
64+
## Feedback, Issues, Pull Requests
65+
66+
The [exercism/haskell](https://github.com/exercism/haskell) repository on
67+
GitHub is the home for all of the Haskell exercises.
68+
69+
If you have feedback about an exercise, or want to help implementing a new
70+
one, head over there and create an issue. We'll do our best to help you!
71+
72+
## Source
73+
74+
Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6)
75+
76+
## Submitting Incomplete Solutions
77+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cabal-version: 1.12
2+
3+
-- This file has been generated from package.yaml by hpack version 0.33.0.
4+
--
5+
-- see: https://github.com/sol/hpack
6+
--
7+
-- hash: e27ffc942adce007d6b37d5bdc51acfedefcefa9ee2e58e92e6b398a73196bdf
8+
9+
name: difference-of-squares
10+
version: 1.2.0.7
11+
build-type: Simple
12+
13+
library
14+
exposed-modules:
15+
Squares
16+
other-modules:
17+
Paths_difference_of_squares
18+
hs-source-dirs:
19+
src
20+
ghc-options: -Wall
21+
build-depends:
22+
base
23+
default-language: Haskell2010
24+
25+
test-suite test
26+
type: exitcode-stdio-1.0
27+
main-is: Tests.hs
28+
other-modules:
29+
Paths_difference_of_squares
30+
hs-source-dirs:
31+
test
32+
build-depends:
33+
base
34+
, difference-of-squares
35+
, hspec
36+
default-language: Haskell2010

difference-of-squares/package.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: difference-of-squares
2+
version: 1.2.0.7
3+
4+
dependencies:
5+
- base
6+
7+
library:
8+
exposed-modules: Squares
9+
source-dirs: src
10+
ghc-options: -Wall
11+
# dependencies:
12+
# - foo # List here the packages you
13+
# - bar # want to use in your solution.
14+
15+
tests:
16+
test:
17+
main: Tests.hs
18+
source-dirs: test
19+
dependencies:
20+
- difference-of-squares
21+
- hspec

difference-of-squares/src/Squares.hs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Squares (difference, squareOfSum, sumOfSquares) where
2+
3+
difference :: Integral a => a -> a
4+
difference n = (squareOfSum n) - (sumOfSquares n)
5+
6+
square :: Integral a => a -> a
7+
square n = n * n
8+
9+
squareOfSum :: Integral a => a -> a
10+
squareOfSum n = square (div (n * (n+1)) 2) ;
11+
12+
sumOfSquares :: Integral a => a -> a
13+
sumOfSquares n = div (n * ((n+1) * ((2*n) + 1))) 6

difference-of-squares/stack.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resolver: lts-15.8

difference-of-squares/stack.yaml.lock

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file was autogenerated by Stack.
2+
# You should not edit this file by hand.
3+
# For more information, please see the documentation at:
4+
# https://docs.haskellstack.org/en/stable/lock_files
5+
6+
packages: []
7+
snapshots:
8+
- completed:
9+
size: 492015
10+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/15/8.yaml
11+
sha256: 926bc3d70249dd0ba05277ff00943c0addb35b627cb641752669e7cf771310d0
12+
original: lts-15.8

difference-of-squares/test/Tests.hs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
2+
3+
import Test.Hspec (Spec, describe, it, shouldBe)
4+
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
5+
6+
import Squares (difference, squareOfSum, sumOfSquares)
7+
8+
main :: IO ()
9+
main = hspecWith defaultConfig {configFastFail = True} specs
10+
11+
specs :: Spec
12+
specs = do
13+
14+
describe "squareOfSum" $ do
15+
it "square of sum 1" $ squareOfSum 1 `shouldBe` 1
16+
it "square of sum 5" $ squareOfSum 5 `shouldBe` 225
17+
it "square of sum 100" $ squareOfSum 100 `shouldBe` 25502500
18+
19+
describe "sumOfSquares" $ do
20+
it "sum of squares 1" $ sumOfSquares 1 `shouldBe` 1
21+
it "sum of squares 5" $ sumOfSquares 5 `shouldBe` 55
22+
it "sum of squares 100" $ sumOfSquares 100 `shouldBe` 338350
23+
24+
describe "differenceOfSquares" $ do
25+
it "difference of squares 1" $ difference 1 `shouldBe` 0
26+
it "difference of squares 5" $ difference 5 `shouldBe` 170
27+
it "difference of squares 100" $ difference 100 `shouldBe` 25164150
28+
29+
-- Track-specific tests.
30+
31+
describe "Integral tests" $ do
32+
33+
describe "squareOfSum" $ do
34+
35+
it "squareOfSum (6 :: Int)" $
36+
squareOfSum (6 :: Int)
37+
`shouldBe` (441 :: Int)
38+
39+
it "squareOfSum (7 :: Integer)" $
40+
squareOfSum (7 :: Integer)
41+
`shouldBe` (784 :: Integer)
42+
43+
describe "sumOfSquares" $ do
44+
45+
it "sumOfSquares (8 :: Int)" $
46+
sumOfSquares (8 :: Int)
47+
`shouldBe` (204 :: Int)
48+
49+
it "sumOfSquares (9 :: Integer)" $
50+
sumOfSquares (9 :: Integer)
51+
`shouldBe` (285 :: Integer)
52+
53+
describe "difference" $ do
54+
55+
it "difference (11 :: Int)" $
56+
difference (11 :: Int)
57+
`shouldBe` (3850 :: Int)
58+
59+
it "difference (12 :: Integer)" $
60+
difference (12 :: Integer)
61+
`shouldBe` (5434 :: Integer)
62+
63+
{-
64+
describe "huge difference" $
65+
it "difference (1234567890 :: Integer)" $
66+
difference (1234567890 :: Integer)
67+
`shouldBe` (580764307309260838625720836817589660 :: Integer)
68+
-}

general-examples/search-file

216 Bytes
Binary file not shown.

general-examples/search-file.hs

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ printSearchResult (a, Just _) = do
1919
printSearchResult (a, Nothing) = do
2020
print $ a <> " is not found"
2121

22-
printSearchResults :: [(String, Maybe Int)] -> IO () -- optional type signature
23-
printSearchResults [] = do
24-
return ()
25-
26-
printSearchResults (result:otherResults) = do
27-
printSearchResult result
28-
printSearchResults otherResults
2922

3023
split :: [Char] -> [Char] -> [[Char]]
3124
split [] _ = []
@@ -49,4 +42,4 @@ main = do
4942
contents <- readFile filePath
5043
let fileWords = split contents ", ?;.\t\n\r"
5144
let searchResults = search inputWords fileWords
52-
printSearchResults searchResults
45+
mapM_ (\r -> printSearchResult r) searchResults

0 commit comments

Comments
 (0)