<div dir="ltr">Also worth noting that Ut's version can be expressed as a fold:<div><br></div><div><font face="monospace">canBalanceElem x acc@(y, z, p) = if p then acc else let y'=y+x; z'=z-x in (y', z', y'==z')<br>canBalanceFold xs = (\(_, _, p) -> p) $ foldr canBalanceElem (0, sum xs, False) xs</font><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 15, 2020 at 10:54 AM Ut Primum <<a href="mailto:utprimum@gmail.com">utprimum@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div>this is how I would have solved the exercise:</div><div><br></div><font face="monospace">canBalanceRec [] _ _ = False<br>canBalanceRec (x:xs) s1 s2 = s1+x==s2-x || canBalanceRec xs (s1+x) (s2-x)<br></font><div><font face="monospace">canBalance xs = s==0 || canBalanceRec xs 0 s where s=sum xs</font></div><div><br></div><div>Note that there is one difference: in this case</div><div><font face="monospace">canBalance [-2,-2,4] = True</font></div><div>because I think I can split the list into [ ] and the rest (since the sum of the empty list is 0 and the same is the sum of the elements of the lists). Anyway this is a matter of how we interpret the text of the exercise (your function would return False instead). Note that removing the "<font face="monospace">s==0 ||</font>" makes no difference. </div><div><br></div><div> An advantage of my solution is that it is less expensive, since its computational complexity is linear in the length of the list xs. Yours uses sum and drop many times, and so is slower. Just to have an idea of the difference (using ghci interpreter):</div><div><br></div><div>> canBalance [1..10000]<br>False<br>(<b>0.02</b> secs, 6,974,552 bytes)<br>> can_split_even [1..10000]<br>False<br>(<b>5.60</b> secs, 11,395,843,384 bytes)<br></div><div><br></div><div>Cheers,</div><div>Ut</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mer 15 gen 2020 alle ore 18:27 Jake Vossen <<a href="mailto:jake@vossen.dev" target="_blank">jake@vossen.dev</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hey everyone,<br>
<br>
Let me know if this is not the right place for this, but I am curious if<br>
someone could take a look at my code and maybe share some feedback / how<br>
a more experienced haskeller would approach this problem.<br>
<br>
New to Haskell, pretty experienced with imperative languages. I have<br>
solved the following problem in Haskell:<br>
<br>
> Given a non-empty array, return true if there is a place to split the<br>
> array so that the sum of the numbers on one side is equal to the sum<br>
> of the numbers on the other side.<br>
<br>
> canBalance([1, 1, 1, 2, 1]) -> true<br>
> canBalance([2, 1, 1, 2, 1]) -> false<br>
> canBalance([10, 10]) -> true<br>
<br>
Here is my code (my solution uses `can_split_even` not `canBalance`)<br>
<br>
```<br>
can_split_even :: (Num a) => (Eq a) => [a] -> Bool<br>
can_split_even xs = True `elem` is_even_at_each_spot<br>
  where<br>
    is_even_at_each_spot :: [Bool]<br>
    is_even_at_each_spot = map (is_split xs) [1 .. (length xs - 1)]<br>
      where<br>
        is_split :: (Num a) => (Eq a) => [a] -> Int -> Bool<br>
        is_split xs index = sum (take index xs) == sum (drop index xs)<br>
```<br>
<br>
Thanks so much!<br>
<br>
-- <br>
Jake Vossen<br>
Colorado School of Mines, Class of 2022<br>
B.S. Computer Science<br>
PGP: 08CD 67DA FE3A 0AE7 B946  6AC3 B812 7052 D9E3 817B<br>
<a href="https://jake.vossen.dev" rel="noreferrer" target="_blank">https://jake.vossen.dev</a><br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>