[Haskell-beginners] Code review request?

Jake Vossen jake at vossen.dev
Wed Jan 15 17:26:31 UTC 2020


Hey everyone,

Let me know if this is not the right place for this, but I am curious if
someone could take a look at my code and maybe share some feedback / how
a more experienced haskeller would approach this problem.

New to Haskell, pretty experienced with imperative languages. I have
solved the following problem in Haskell:

> Given a non-empty array, return true if there is a place to split the
> array so that the sum of the numbers on one side is equal to the sum
> of the numbers on the other side.

> canBalance([1, 1, 1, 2, 1]) -> true
> canBalance([2, 1, 1, 2, 1]) -> false
> canBalance([10, 10]) -> true

Here is my code (my solution uses `can_split_even` not `canBalance`)

```
can_split_even :: (Num a) => (Eq a) => [a] -> Bool
can_split_even xs = True `elem` is_even_at_each_spot
  where
    is_even_at_each_spot :: [Bool]
    is_even_at_each_spot = map (is_split xs) [1 .. (length xs - 1)]
      where
        is_split :: (Num a) => (Eq a) => [a] -> Int -> Bool
        is_split xs index = sum (take index xs) == sum (drop index xs)
```

Thanks so much!

-- 
Jake Vossen
Colorado School of Mines, Class of 2022
B.S. Computer Science
PGP: 08CD 67DA FE3A 0AE7 B946  6AC3 B812 7052 D9E3 817B
https://jake.vossen.dev


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200115/2ba860f8/attachment.sig>


More information about the Beginners mailing list