Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Code review request? (Akhra Gannon) 2. Re: Code review request? (Julian Camilo Osorio) 3. Parallel Processing in Libraries? (Leonhard Applis) ---------------------------------------------------------------------- Message: 1 Date: Wed, 15 Jan 2020 12:02:04 -0800 From: Akhra Gannon <tan...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Code review request? Message-ID: <CAJoPsuAvJGjS1jKARXp8xTwpZo1A2-uDE1OZEazAY=jxxmv...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Also worth noting that Ut's version can be expressed as a fold: canBalanceElem x acc@(y, z, p) = if p then acc else let y'=y+x; z'=z-x in (y', z', y'==z') canBalanceFold xs = (\(_, _, p) -> p) $ foldr canBalanceElem (0, sum xs, False) xs On Wed, Jan 15, 2020 at 10:54 AM Ut Primum <utpri...@gmail.com> wrote: > Hi, > this is how I would have solved the exercise: > > canBalanceRec [] _ _ = False > canBalanceRec (x:xs) s1 s2 = s1+x==s2-x || canBalanceRec xs (s1+x) (s2-x) > canBalance xs = s==0 || canBalanceRec xs 0 s where s=sum xs > > Note that there is one difference: in this case > canBalance [-2,-2,4] = True > 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 > "s==0 ||" makes no difference. > > 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): > > > canBalance [1..10000] > False > (*0.02* secs, 6,974,552 bytes) > > can_split_even [1..10000] > False > (*5.60* secs, 11,395,843,384 bytes) > > Cheers, > Ut > > Il giorno mer 15 gen 2020 alle ore 18:27 Jake Vossen <j...@vossen.dev> ha > scritto: > >> 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 >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200115/2bf901f4/attachment-0001.html> ------------------------------ Message: 2 Date: Wed, 15 Jan 2020 16:05:06 -0500 From: Julian Camilo Osorio <juliancamilo.oso...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Code review request? Message-ID: <CAHz-aL34soF4kSPZQD1iCZ+5JuHWoh5d16fPxccAGPVV-P+=j...@mail.gmail.com> Content-Type: text/plain; charset="UTF-8" For what it's worth, this is an equivalent one-liner: canBalance xs = any ((== sum xs) . (*2)) (scanl (+) 0 xs) On Wed, 15 Jan 2020 at 15:02, Akhra Gannon <tan...@gmail.com> wrote: > > Also worth noting that Ut's version can be expressed as a fold: > > canBalanceElem x acc@(y, z, p) = if p then acc else let y'=y+x; z'=z-x in > (y', z', y'==z') > canBalanceFold xs = (\(_, _, p) -> p) $ foldr canBalanceElem (0, sum xs, > False) xs > > > On Wed, Jan 15, 2020 at 10:54 AM Ut Primum <utpri...@gmail.com> wrote: >> >> Hi, >> this is how I would have solved the exercise: >> >> canBalanceRec [] _ _ = False >> canBalanceRec (x:xs) s1 s2 = s1+x==s2-x || canBalanceRec xs (s1+x) (s2-x) >> canBalance xs = s==0 || canBalanceRec xs 0 s where s=sum xs >> >> Note that there is one difference: in this case >> canBalance [-2,-2,4] = True >> 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 "s==0 ||" makes >> no difference. >> >> 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): >> >> > canBalance [1..10000] >> False >> (0.02 secs, 6,974,552 bytes) >> > can_split_even [1..10000] >> False >> (5.60 secs, 11,395,843,384 bytes) >> >> Cheers, >> Ut >> >> Il giorno mer 15 gen 2020 alle ore 18:27 Jake Vossen <j...@vossen.dev> ha >> scritto: >>> >>> 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 >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 3 Date: Thu, 16 Jan 2020 10:15:32 +0000 From: Leonhard Applis <leonhard.app...@protonmail.com> To: "beginners@haskell.org" <beginners@haskell.org> Subject: [Haskell-beginners] Parallel Processing in Libraries? Message-ID: <2MKFj_uwsGLJVa39UEnu8rSYWGYLs2thwMKEytWB6FyyH82uI6dR1qCai3NNOpLC3wJiJ_GHBMkAfyY6WYhRwDqsmleDInE7R7KSELQqPQc=@protonmail.com> Content-Type: text/plain; charset="utf-8" Hi, I'm starting to dip into Parallel Processing with Haskell while reading "Parallel and Concurrent Programming in Haskell". All the Examples in the Book construct a program which is doing all the work, so with all files in the same program which has a main method. I'd like to have a library which utilized parallel programming (mostly for map-reduce tasks). Is this possible? My first approach of putting the par-code in my library and running the program threaded does not seem to use multiple cores. Also I have not seen any parallel programming in "popular" libraries (such as QuickCheck) which makes me think that I'm on the wrong path. To summarize my goal: I want to build a computation-intensive library, which utilizes multiple cores when used from an executable. Thank you Leonhard -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200116/5b25a37c/attachment-0001.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: publickey - leonhard.app...@protonmail.com - 0x807FDDF3.asc Type: application/pgp-keys Size: 1843 bytes Desc: not available URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200116/5b25a37c/attachment-0001.key> -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 477 bytes Desc: OpenPGP digital signature URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200116/5b25a37c/attachment-0001.sig> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 139, Issue 5 *****************************************