Logs: liberachat/#haskell
| 2021-08-22 08:38:02 | <Guest9> | I am trying to create a function which adds all digits to a single number |
| 2021-08-22 08:38:42 | <Hecate> | Guest9: so, 'x + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9'? |
| 2021-08-22 08:38:42 | <Guest9> | digitSum = sum . map (read . return) . show |
| 2021-08-22 08:39:06 | <Guest9> | this is the function I wrote and it works.. |
| 2021-08-22 08:39:33 | <Guest9> | but now I want to make it such that it adds the result also to a single digit |
| 2021-08-22 08:41:00 | <Guest9> | singleDigitSum digitsum |
| 2021-08-22 08:41:00 | <Guest9> | | digitsum <= 9 = a |
| 2021-08-22 08:41:01 | <Guest9> | | otherwise digitsum a |
| 2021-08-22 08:41:10 | <Guest9> | so I wrote the above function |
| 2021-08-22 08:41:25 | × | severen quits (~severen@202.36.179.72) (Ping timeout: 252 seconds) |
| 2021-08-22 08:41:33 | <Guest9> | singleDigitSum digitsum |
| 2021-08-22 08:41:34 | <Guest9> | | digitsum a <= 9 = a |
| 2021-08-22 08:41:34 | <Guest9> | | otherwise digitsum a |
| 2021-08-22 08:41:40 | <Guest9> | sorry typo |
| 2021-08-22 08:41:44 | <flairif> | thanks :) I'm creating a little number guessing game.. I have `num :: IO (Int)` as the random number... I understand that it's not a "global variable" like in other languages... Everytime I do `n <- num` it's assigning a new random number to n, which is not quite what I want. |
| 2021-08-22 08:42:27 | <Hecate> | flairif: the key here is "variable" |
| 2021-08-22 08:42:33 | <flairif> | My question is difficult to ask, because its kinda contextual... |
| 2021-08-22 08:42:50 | <Hecate> | it's a binding, which is accessible from everywhere (with scoping) |
| 2021-08-22 08:42:58 | <Hecate> | but it's indeed not mutable |
| 2021-08-22 08:43:33 | <[exa]> | Guest9: so your function should take a _list_ of numbers in strings (?) and add them together? |
| 2021-08-22 08:43:40 | × | favonia quits (~favonia@user/favonia) (Ping timeout: 240 seconds) |
| 2021-08-22 08:43:48 | <[exa]> | Guest9: or just sum 1 to 9 ? |
| 2021-08-22 08:43:59 | <[exa]> | Guest9: or sum all digits of some number? |
| 2021-08-22 08:44:14 | <Guest9> | @ no.. it just takes an number.. reads all digits and adds them up.. |
| 2021-08-22 08:44:32 | <flairif> | Right. I guess what I am trying to get at is, how would I "store" a single randomly generated Int that can be used be used throughout the entirity of the program? |
| 2021-08-22 08:44:40 | <Guest9> | except that when the sum is more than 9 then it will add those also till it reaches a single digit.. |
| 2021-08-22 08:44:50 | <Guest9> | actually I am trying to learn recursion..: ) |
| 2021-08-22 08:45:03 | <Hecate> | flairif: you can pass it as argument :) |
| 2021-08-22 08:45:05 | <[exa]> | Guest9: ah so. that explains the `return` there. You might have better luck with list comprehension, such as `sumDigits x = sum [read x | x <- show x]` |
| 2021-08-22 08:45:30 | <[exa]> | Guest9: note: creating lists that you immediately consume is basically free in haskell, so no worries about an intermediate list |
| 2021-08-22 08:45:52 | <[exa]> | btw I've got a mistake there, should be `read [x]` ofc. |
| 2021-08-22 08:46:07 | <[exa]> | and even better name the `x` differently than the original `x`... :D |
| 2021-08-22 08:46:59 | <[exa]> | :t \x -> sum [read [c] | c <- show x] |
| 2021-08-22 08:47:00 | <lambdabot> | (Num a1, Show a2, Read a1) => a2 -> a1 |
| 2021-08-22 08:47:06 | <flairif> | Hecate: I suppose. It just feels uncomfortable, because what if my program was aribrarily large, with dozens of functions that all wanted access to that number, hypothetically? |
| 2021-08-22 08:47:26 | <[exa]> | flairif: you might be looking for State or Reader monads |
| 2021-08-22 08:47:33 | <[exa]> | these are pretty much contextural |
| 2021-08-22 08:48:03 | <Hecate> | flairif: then you would take the time to explore such options by setting the boundaries of your requirements with these parameters ;) |
| 2021-08-22 08:48:18 | <flairif> | hmmm |
| 2021-08-22 08:48:21 | <Hecate> | and you would go towards MVar for example |
| 2021-08-22 08:48:52 | <Hecate> | but for a guessing game it's extremely simple to have the number as an argument, especially if it's a bounded Int |
| 2021-08-22 08:50:03 | → | merijn joins (~merijn@83-160-49-249.ip.xs4all.nl) |
| 2021-08-22 08:50:40 | <Guest9> | @[exa] : This would be lambda expressions ?.. I guess yet to reach there.. I am trying to write a toy program from C++ where in you take a list of dates and come out with a date number, month number and year number.. using mod operator.. a numerology program.. =D |
| 2021-08-22 08:50:40 | <lambdabot> | Unknown command, try @list |
| 2021-08-22 08:51:01 | <Guest9> | There are no exercises in LYAH.. |
| 2021-08-22 08:51:58 | <[exa]> | LYAH doesn't have many exercises, sadly, yes |
| 2021-08-22 08:52:28 | <[exa]> | anyway yeah the \ is a lambda, you can equivalently have a named definition as `sumDigits x = sum [read [c] | c <- show x]` |
| 2021-08-22 08:52:29 | <flairif> | Hecate, hmmm ok. I'll try it |
| 2021-08-22 08:54:40 | × | kenran quits (~kenran@200116b82b8bf8008c77ec945bde89d6.dip.versatel-1u1.de) (Ping timeout: 240 seconds) |
| 2021-08-22 08:55:00 | <Guest9> | yes.. but `sumDigits x = sum [read [c] | c <- show x]` gives a two digit answer when supplied with a string.. |
| 2021-08-22 08:55:34 | <Guest9> | Prelude> sumDigits 987458 |
| 2021-08-22 08:55:37 | → | kenran joins (~kenran@200116b82b8bf8007bc477e0c5eee3d0.dip.versatel-1u1.de) |
| 2021-08-22 08:55:39 | <Guest9> | output is 41 |
| 2021-08-22 08:55:58 | <[exa]> | that's right isn't it? |
| 2021-08-22 08:56:28 | <[exa]> | I might have misunderstood the original goal then if not :] |
| 2021-08-22 08:56:55 | <Hecate> | flairif: https://replit.com/@Kleidukos/GreenyellowCyanProgrammer#src/Main.hs |
| 2021-08-22 08:57:22 | <Hecate> | flairif: and type "stack run src/Main.hs" in the "Shell" tab |
| 2021-08-22 08:57:22 | × | Sgeo quits (~Sgeo@user/sgeo) (Ping timeout: 252 seconds) |
| 2021-08-22 08:59:13 | <flairif> | Ok cool |
| 2021-08-22 09:01:18 | × | burnsidesLlama quits (~burnsides@dhcp168-023.wadham.ox.ac.uk) (Remote host closed the connection) |
| 2021-08-22 09:01:22 | <flairif> | Hecate, I came up with a solution very similar to that one, so that's nice haha |
| 2021-08-22 09:02:42 | × | eggplantade quits (~Eggplanta@108-201-191-115.lightspeed.sntcca.sbcglobal.net) (Remote host closed the connection) |
| 2021-08-22 09:03:09 | → | o1lo01ol1o joins (~o1lo01ol1@5.181.115.89.rev.vodafone.pt) |
| 2021-08-22 09:06:37 | <Guest9> | is there a recursive way to solve this problem ? |
| 2021-08-22 09:07:49 | × | o1lo01ol1o quits (~o1lo01ol1@5.181.115.89.rev.vodafone.pt) (Ping timeout: 252 seconds) |
| 2021-08-22 09:08:22 | × | econo quits (uid147250@user/econo) (Quit: Connection closed for inactivity) |
| 2021-08-22 09:14:04 | → | ubert joins (~Thunderbi@91.141.64.154.wireless.dyn.drei.com) |
| 2021-08-22 09:14:44 | × | Guest1719 quits (~chris@81.96.113.213) (Remote host closed the connection) |
| 2021-08-22 09:15:41 | → | o1lo01ol1o joins (~o1lo01ol1@5.181.115.89.rev.vodafone.pt) |
| 2021-08-22 09:17:36 | <[exa]> | Guest9: what should be the result for 987458? |
| 2021-08-22 09:17:40 | × | flairif quits (~user@2603-90d8-0b40-c000-9a65-0d16-0508-f6c0.res6.spectrum.com) (Ping timeout: 240 seconds) |
| 2021-08-22 09:19:22 | × | ubert quits (~Thunderbi@91.141.64.154.wireless.dyn.drei.com) (Ping timeout: 252 seconds) |
| 2021-08-22 09:19:24 | × | hnOsmium0001 quits (uid453710@id-453710.stonehaven.irccloud.com) (Quit: Connection closed for inactivity) |
| 2021-08-22 09:22:15 | → | jgeerds joins (~jgeerds@55d4b311.access.ecotel.net) |
| 2021-08-22 09:22:49 | × | o1lo01ol1o quits (~o1lo01ol1@5.181.115.89.rev.vodafone.pt) (Remote host closed the connection) |
| 2021-08-22 09:22:55 | → | Gurkenglas joins (~Gurkengla@dslb-002-203-144-156.002.203.pools.vodafone-ip.de) |
| 2021-08-22 09:24:10 | × | merijn quits (~merijn@83-160-49-249.ip.xs4all.nl) (Ping timeout: 250 seconds) |
| 2021-08-22 09:26:02 | × | phma quits (phma@2001:5b0:2144:518:c685:6b7:63c5:83c0) (Read error: Connection reset by peer) |
| 2021-08-22 09:26:58 | → | phma joins (phma@2001:5b0:211f:c4a8:b4c0:8312:139:73b8) |
| 2021-08-22 09:31:06 | × | azeem quits (~azeem@dynamic-adsl-94-34-33-6.clienti.tiscali.it) (Ping timeout: 250 seconds) |
| 2021-08-22 09:31:08 | <Guest9> | 5 |
| 2021-08-22 09:31:35 | → | azeem joins (~azeem@176.200.245.90) |
| 2021-08-22 09:32:02 | <Guest9> | 9+8+7+4+5+8 = 41.. 4+1 =5 |
| 2021-08-22 09:33:35 | × | azeem quits (~azeem@176.200.245.90) (Read error: Connection reset by peer) |
| 2021-08-22 09:34:02 | → | azeem joins (~azeem@dynamic-adsl-94-34-33-6.clienti.tiscali.it) |
| 2021-08-22 09:34:14 | → | burnsidesLlama joins (~burnsides@dhcp168-023.wadham.ox.ac.uk) |
| 2021-08-22 09:34:37 | → | jtomas joins (~jtomas@233.red-83-34-2.dynamicip.rima-tde.net) |
| 2021-08-22 09:34:46 | × | geekosaur quits (~geekosaur@xmonad/geekosaur) (Ping timeout: 252 seconds) |
| 2021-08-22 09:36:32 | <dminuoso> | Guest9: Is this a homework assignment? |
| 2021-08-22 09:36:47 | <Guest9> | nope |
| 2021-08-22 09:37:02 | <Guest9> | I am taking Cpp problems and doing in haskell |
| 2021-08-22 09:37:15 | <Guest9> | as I said earlier.. no examples in LYAH |
| 2021-08-22 09:37:33 | → | geekosaur joins (~geekosaur@xmonad/geekosaur) |
| 2021-08-22 09:38:28 | × | burnsidesLlama quits (~burnsides@dhcp168-023.wadham.ox.ac.uk) (Ping timeout: 250 seconds) |
| 2021-08-22 09:39:34 | <dminuoso> | Guest9: start by writing a function `digits :: Integer -> [Integer]` perhaps? |
| 2021-08-22 09:39:51 | → | reumeth joins (~reumeth@user/reumeth) |
| 2021-08-22 09:40:02 | <dminuoso> | From there, you can proceed with writing some simple recursive function. |
| 2021-08-22 09:40:36 | <dminuoso> | % sum digits 1234 |
| 2021-08-22 09:40:37 | <yahb> | dminuoso: ; <interactive>:4:5: error:; * Couldn't match type: [x0]; with: t0 -> t; Expected: x0 -> t0 -> t; Actual: x0 -> [x0]; * In the first argument of `sum', namely `digits'; In the expression: sum digits 1234; In an equation for `it': it = sum digits 1234; * Relevant bindings include it :: t (bound at <interactive>:4:1) |
| 2021-08-22 09:40:39 | <dminuoso> | % sum (digits 1234) |
All times are in UTC.