Skip to content

Instantly share code, notes, and snippets.

@bolerap
Created January 7, 2019 02:18
Show Gist options
  • Save bolerap/a0a865416ac71578faf82239b88d7bce to your computer and use it in GitHub Desktop.
Save bolerap/a0a865416ac71578faf82239b88d7bce to your computer and use it in GitHub Desktop.
-- Simple.hs
-- calculates the summary of a sequence number with a pattern
-- eg: [1, 2, 3, 4] or [1, 3, 5, 7] or [1, 3, 6, 9]
-- general form [a..b] => sum = ((a + b) * length [a..b]) / 2
seqSum :: Fractional a => [a] -> a
seqSum list = ((first + last) * len) / 2
where
len = length list
first = head list
last = list !! (len - 1)
@bolerap
Copy link
Author

bolerap commented Jan 7, 2019

got error

Simple.hs:104:33: error:
    • Couldn't match expected type ‘a’ with actual type ‘Int’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          seqSum :: forall a. Fractional a => [a] -> a
        at Simple.hs:103:1-34
    • In the second argument of ‘(*)’, namely ‘len’
      In the first argument of ‘(/)’, namely ‘((first + last) * len)’
      In the expression: ((first + last) * len) / 2
    • Relevant bindings include
        last :: a (bound at Simple.hs:108:5)
        first :: a (bound at Simple.hs:107:5)
        list :: [a] (bound at Simple.hs:104:8)
        seqSum :: [a] -> a (bound at Simple.hs:104:1)
    |
104 | seqSum list = ((first + last) * len) / 2
    |                                 ^^^
Failed, no modules loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment