Тёмный

Treating Lists as Monads - HaskellRank Ep.11 

Tsoding
Подписаться 32 тыс.
Просмотров 14 тыс.
50% 1

Опубликовано:

 

29 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 57   
@zeljkobekcic9656
@zeljkobekcic9656 5 лет назад
I really like your videos and it really got me into haskell, now I am able to solve some problems in haskell too. Thank you for that. But I have a question: Why did not you add the (-1) to the list after the filter and then apply the maximum function?
@Tsoding
@Tsoding 5 лет назад
I don't know. Maybe because I'm actually dumb. This is a genius idea! Thanks!
@johngalmann9579
@johngalmann9579 5 лет назад
Technically the exercises never garuanteed you couldn't have negative prices, thus -1 could be the maximum without being the only element in the list.
@Tsoding
@Tsoding 5 лет назад
@@johngalmann9579 It did under the "Constraints" section www.hackerrank.com/challenges/electronics-shop/problem
@MdImrulHassan
@MdImrulHassan 5 лет назад
That's a neat idea. I was think mapping values over the threshold to -1 and then apply maximum. However, I like the solution in the video more as it makes it clear by using Maybe whether we found a solution or not and then apply the default value at a later stage. The only part I wish was done differently is not using the sort. However, the constraint is quite low, so that kind of optimization might have been overkill.
@clamiam45
@clamiam45 5 лет назад
I thought about this too, but then thought that it's nice to learn how to work with maybes. :)
@ZachariahCavazos
@ZachariahCavazos 5 лет назад
༼ つ ◕_◕ ༽つ simply beautiful. My favorite part as always is how you break down large problems into small problems. This gives me the confidence to solve problems in Haskell
@lambdaway
@lambdaway 3 года назад
Awesome content! As list also Alternative, we can simplify solve function: maximum $ filter (
@anguszh
@anguszh 5 лет назад
⏎ foldl max (-1) [k + d | k
@Bratjuuc
@Bratjuuc 4 года назад
"foldl max" has the same effect as "maximum"
@hrishikeshkulkarni2320
@hrishikeshkulkarni2320 4 года назад
Superb video and thank you for demonstrating not just an elegant solution but also monadic nature of the simple list in Haskell. I really wished that you could have chosen a different problem for that demonstration, though. This particular problem involved a cross of two lists, implying O(N^2) time complexity. It can be solved in asymptotically faster time by sorting and traversal, even with Haskell though it won't look as pretty. The input size of just 1000 for this problem made this pass.
@123coolmik
@123coolmik 5 лет назад
You can also use List as an Applicative: (,) keyboard drives
@christianbrolin1482
@christianbrolin1482 5 лет назад
maximum $ (-1:) $ filter (
@ПётрМакаров-о8т
@ПётрМакаров-о8т 4 года назад
liftA2 (,) keyboard drives
@MoeZarella
@MoeZarella 5 лет назад
this may not be of concern, but isn't sorting the whole list (n log n) less efficient than just taking the minimum (n)? (after filtering out empty lists e.g. by pattern matching)
@Tsoding
@Tsoding 5 лет назад
Yeah, I agree. But writting long uninterrupted function compositions is so addictive. :)
@MoeZarella
@MoeZarella 5 лет назад
@@Tsoding awesome oneliners is what this series is known for! :D
@johngalmann9579
@johngalmann9579 5 лет назад
I don't know what algorithm Haskell uses for sorting, but since it's lazy it won't necessarily sort the whole list, as long as it gets the first element correct.
@MoeZarella
@MoeZarella 5 лет назад
@@johngalmann9579 yeah possible
@BrianMcKennaPuffnfresh
@BrianMcKennaPuffnfresh 5 лет назад
That's the beauty of laziness. They're the same complexity: O(n) - so with laziness, function composition gives rise to algorithmic composition.
@ChongFrisbee
@ChongFrisbee 5 лет назад
Hello! I really like this series. Thanks for making them. I got stumped by the "Array Manipulation" problem. I'm confident my code works, but it times out on several tests. I don't know if you're interested in scaling up the difficulty of the problems in the series. If you are, I'm just going to leave this one as a suggestion.
@DanDart
@DanDart 4 года назад
when you think of zip, see zipWith and think... hmmm I could go more generic!
@afnanenayet8788
@afnanenayet8788 4 года назад
zipWith wouldn’t work because it won’t give you the Cartesian product of the two lists,
@spyr0th3dr4g0n
@spyr0th3dr4g0n 5 лет назад
Why not concatenate a -1 to the result of the filter, guaranteeing it is not empty for the maximum function?
@DanDart
@DanDart 4 года назад
Dots, and "print"!
@MrHatoi
@MrHatoi 4 года назад
7:03 Can't you also solve the -1 problem by adding "(-1) : " right before filter? That way the list is never empty, and since all of the other values are positive the maximum function will never select -1 unless there are no other values in the list.
@HelloForeignWorld
@HelloForeignWorld 4 года назад
My though exactly 👍🏻
@fimmind
@fimmind 5 лет назад
You can use *print* function instead of *putStrLn . show*
@aBamieh
@aBamieh 5 лет назад
Awesome! thank you
@DanDart
@DanDart 5 лет назад
I quite often like "print" rather than "putStrLn $ show"
@BasiC7786
@BasiC7786 5 лет назад
any reason not to use replicateM for certesian product? I think that's a bit more inuitive when teaching people what the List Monad instance does.
@christianbrolin1482
@christianbrolin1482 5 лет назад
How?
@clementdato6328
@clementdato6328 2 года назад
Maybe should use a O(nlogn) problem? otherwise it would be trivial to write something tidy… like in whatever languages, at most two loops?
@ikezedev
@ikezedev 4 года назад
Gracious 😍
@skepticmoderate5790
@skepticmoderate5790 4 года назад
Why isn't listToMaybe just called safeHead or totalHead?
@christianbrolin1482
@christianbrolin1482 5 лет назад
maximum $ (-1:) $ filter (
@AaronTheGerman
@AaronTheGerman 5 лет назад
maximum $ filter (
@MrTyler918273
@MrTyler918273 4 года назад
maximum $ (-1:) [k+d | k
@arturgajowy
@arturgajowy 4 года назад
safeMaximum = fmap getMax . foldMap (Just . Max)
@MrTyler918273
@MrTyler918273 5 лет назад
Introducing too many complicated things for a simple problem. Prices and budgets are never negative so just throw a -1 in the list and you are basically done. Plus you can use a list comprehension rather than liftM, which I find more readable and less obscure even though they are the same thing under the hood. solve b keyboards drives = maximum $ -1 : [z | x
@jadissa3841
@jadissa3841 5 лет назад
You can just use print whatever Instead of putStrLn $ show whatever
@spherinder5793
@spherinder5793 5 лет назад
Your mic is too close, I can't listen when the audio is too asmr style.
@Tsoding
@Tsoding 5 лет назад
Oh, wow! It's actually a thing! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-XrFegNHpHfc.html
@greob
@greob 5 лет назад
Nice, thanks for sharing!
Далее
The Tragedy of systemd
47:18
Просмотров 1,1 млн
Haskell in 100 Seconds
2:30
Просмотров 875 тыс.
What is a Monad? - Computerphile
21:50
Просмотров 606 тыс.
What is IO monad?
36:32
Просмотров 70 тыс.
Using Monad Transformers without understanding them
24:23
Emacs. But why?
51:20
Просмотров 4,5 тыс.
The Monad Problem
23:53
Просмотров 2,2 тыс.
Is this the Future of Programming Languages?
2:53:18
Просмотров 69 тыс.
Between Two Sets -- HaskellRank #05
14:07
Просмотров 22 тыс.