Between Two Sets -- HaskellRank #05

Поділитися
Вставка
  • Опубліковано 7 січ 2025

КОМЕНТАРІ • 37

  • @smuecke
    @smuecke 6 років тому +37

    I think you actually implemented foldl1 as foldr1, but it still works because lcm and gcd are commutative.

    • @Tsoding
      @Tsoding  6 років тому +12

      Thanks for pointing that out! Yes, I agree that I might have created confusion with different kinds of folds in this video. I'm going to address that in the next one. :)

    • @noamtashma617
      @noamtashma617 6 років тому +8

      Technically, it doesn't matter which fold you choose because gcd and lcd are associative. They're also commutative, but that is unrelated.

    • @Tsoding
      @Tsoding  6 років тому +4

      You may find this video interesting ua-cam.com/video/24XK4LPoCXc/v-deo.html

  • @davidyanceyjr
    @davidyanceyjr 6 років тому +16

    I've learned more since starting my journey with haskell 3 months ago than a years worth of imperative languages. Evidently they're not actually imperative. Love the videos. Keep them coming please.

  • @WeatheRay
    @WeatheRay 6 років тому +12

    These are great. Thank you.
    A note on [1 .. ]. Haskell does not remember the previous number in the list, that is, it doesn't keep that in memory. Once the number is displayed to your screen, the memory is released, allowing you to print an infinite list and exit it at your leisure.

    • @chaddaifouche536
      @chaddaifouche536 4 роки тому +1

      You're right but if you print `xs` like here, you're holding onto the head of your list so it can't be garbage collected... Which means occupied memory will keep increasing until there's not enough to continue.
      When you print an infinite list without putting it into a variable (or without using the variable afterward in a function), it will often get optimized to a tight loop in assembly anyway (if you compile your code).

  • @az-qf9ht
    @az-qf9ht 6 років тому +5

    If I wanted to continue thinking imperatively, I would not be learning Haskell. Keep teaching Haskell!

  • @nickboyadjian367
    @nickboyadjian367 6 років тому +5

    These are gold, man. Keep it up. Much love from America

  • @renecura
    @renecura 4 роки тому +1

    "Lets do some imperative programming... with implicit recursion!"

  • @Sam_Derbyshire
    @Sam_Derbyshire 6 років тому

    To count numbers which are simultaneously multiples of asLcm and divisors of bsGcd, you can count the number of divisors of the quotient bsGcd / asLcm (return 0 if asLcm doesn't divide bsGcd).
    To count divisors of a number, you can do the obvious thing (filter a list of candidates, take the length), or use multiplicativity of the number of divisors. A prime power p^a has a+1 divisors, and the number of divisors of a product of coprime numbers is the product of the number of divisors.
    For instance, the number of divisors of 16 = 2^4 is 4+1=5, the number of divisors of 27 = 3^3 is 3+1=4, the number of divisors of 432 = 2^4 * 3^3 is 5 * 4 = 20.

  • @Divyansh71
    @Divyansh71 6 років тому +1

    Great video as always! waiting for more from the series :D

  • @FernandoBasso
    @FernandoBasso 5 років тому

    C-x SPACE enters the rectangle mode, move up and down to make the rectangle spawn across multiple likes. How did you insert "l1" without M-x string-insert-rectangle ? Did you just do C-t ?

  • @top_cat26
    @top_cat26 6 років тому +1

    muy bueno ....gracias¡¡¡

  • @bjornkihlberg2103
    @bjornkihlberg2103 6 років тому

    "fold" mmm ;) You sneaky devil!

  • @Hammaddev-b3j
    @Hammaddev-b3j Рік тому

    here's one that i came up with, a bit more elegant
    slove :: [Int] -> [Int] -> Int
    solve as bs
    | gcd' `isMultipleOf` lcm' = length [lcm',2*lcm'...gcd']
    | otherwise = 0
    where lcm' = fold lcm as
    gcd' = fold gcd bs
    isMultipleOf = (==0) . mod

  • @johnnyphoney5669
    @johnnyphoney5669 6 років тому +12

    Something wrong with you, don't you wanna to try dangerous stuff? Just run that [1..] and show to the kids how it blows up! Anyway, you would have a lot of time to interrupt it by ^C.

    • @Tsoding
      @Tsoding  6 років тому +4

      The last time I tried that inside of Emacs ^C didn't work for some reason. :(

    • @johnnyphoney5669
      @johnnyphoney5669 6 років тому

      P.S. When you're looking at it, rows of numbers are shifting, filling a pattern, you could see the matrix, the god, why didn't you share this magic toy with the world?

    • @dylutante
      @dylutante 6 років тому +3

      It has to do with the fact there are many Emacs keybindings starting with Ctrl + C (most of the major mode commands). The ^C in emacs shells/REPLs/etc is actually done with ^C^C (that's right: the same thing done twice).

  • @fluffy_tail4365
    @fluffy_tail4365 6 років тому +1

    wait, you implemented foldr1, not foldl1, right?

    • @Tsoding
      @Tsoding  6 років тому

      Yes, I might have created confusion with different kinds of folds in this video. I'm going to address that in the next one. :)

  • @asdfghyter
    @asdfghyter 6 років тому +1

    I believe that your fold is actually foldr1, not foldl1, since the innermost elements are to the right. hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.List.html#foldr1
    foldl1 (+) [1..4] corresponds to ((1 + 2) + 3) + 4
    foldr1 (+) [1..4] corresponds to 1 + (2 + (3 + 4))
    Foldl would look like this:
    foldl :: (b -> a -> b) -> b -> [a] -> [b]
    foldl f acc (x:xs) = foldl f (f acc x) xs
    foldl f acc [] = acc

    • @Tsoding
      @Tsoding  6 років тому +1

      Thanks for pointing that out! Yes, I agree that I might have created confusion with different kinds of folds in this video. I'm going to address that in the next one. :)

    • @asdfghyter
      @asdfghyter 6 років тому

      Sounds great! Thanks for the videos!

  • @voolm1
    @voolm1 6 років тому

    The way you explain takeWhile would work with the filter function, you should have used another instance.
    Great video nonetheless !

  • @ЛюсяКаракеш
    @ЛюсяКаракеш 4 роки тому

    красава, четкие уроки у тебя

  • @shashankkumar851
    @shashankkumar851 4 роки тому

    until i searched its solution on youtube , i was thinking how stupid i might be as unable to solve an easy category question, hackerrank shouldnt have put this in easy category.

  • @lumed2189
    @lumed2189 4 роки тому

    Clean cose😍😍😍

  • @KingZero69
    @KingZero69 5 років тому +1

    gg

  • @AhmedKhaled-sj1zx
    @AhmedKhaled-sj1zx 6 років тому

    So Haskell is not pure ?!

    • @Tsoding
      @Tsoding  6 років тому +3

      Well, I guess otherwise it would've been useless. :)

    • @AhmedKhaled-sj1zx
      @AhmedKhaled-sj1zx 6 років тому

      Yeah, I know some of what you said.
      Actually i'm lisper -newbie lisper- and when I read that (haskell is PURE and lisp is not) in this point I feel like lisp betrayal of truth of functional way and you know other stuff.
      I know -not sure- that lisp is pure by default but you have power to you know have mutable var and stuff like that

    • @aymanayman9000
      @aymanayman9000 5 років тому +1

      @@AhmedKhaled-sj1zx how do you learn lisp I saw it in course called programming languages on coursers but in racket dress and it is weird I saw in the same course language called sml and it has this brilliant feature pattern marching

    • @AhmedKhaled-sj1zx
      @AhmedKhaled-sj1zx 5 років тому +1

      @@aymanayman9000
      Hello Ayman, hope yoi doing well in Ramada, I the course you are talk about, Prof. Don actually didn't explain the lisp feature well, because it's not the course interest. I know lisp from book called SICP. And I use 3 or 4 programs written in lisp (most of them is scheme lisp). SICP ~was~ is a very dense/rich/elegant programming textbook for beginners. And it is my way into lisp (scheme lisp)

    • @aymanayman9000
      @aymanayman9000 5 років тому

      @@AhmedKhaled-sj1zx thanks

  • @shikharkr
    @shikharkr 5 років тому

    {- Solution using List comprehension -}
    let cs = [ x | x