Skip to content

Instantly share code, notes, and snippets.

@quephird
Last active September 4, 2015 20:31
Show Gist options
  • Save quephird/e54612c1d6ff4538a48b to your computer and use it in GitHub Desktop.
Save quephird/e54612c1d6ff4538a48b to your computer and use it in GitHub Desktop.
Solution to exercise 2 in section 8.7 in PureScript by Example
module Sums where
import Data.Array ((:), foldM, nub, sort)
import Data.Maybe (Maybe(..))
import Prelude (($), (+), (++), map)
-- Given an array of integers, compute the list of all
-- possible totals, using each integer at most once.
sums :: Array Int -> Maybe (Array Int)
sums = foldM (\a e -> Just $ sort $ nub $ (e :) $ map (+e) a ++ a) [0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment