Haskell for Imperative Programmers #19 - Infinite Lists

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 23

  • @philipphagenlocher
    @philipphagenlocher  4 года назад +19

    Correction: At 2:55 the correct definition for odds is "odds = filter (\x -> mod x 2 == 1) nat".

    • @YNEzra
      @YNEzra 2 года назад

      Shouldn't the anonymous function for map also have an \x?

  • @UnbezahlbareFreiheit
    @UnbezahlbareFreiheit 4 года назад +26

    Great video series! One nitpick: The naive recursive implementation of fib doesn't grow quadratic, but exponential.

    • @NaderHGhanbari
      @NaderHGhanbari 3 года назад +5

      Yes :) Just like Fibonacci numbers themselves, because the naive impl is exactly like finger counting.

    • @NewYorkFreeman
      @NewYorkFreeman 10 месяцев назад +2

      Indeed, with the base being Fibonacci constant raised to n.

  • @Merlin-gl7zp
    @Merlin-gl7zp 2 месяца назад +1

    Unfortunately `length ones` freezes ghci :>. + fun fact: ghci starts the compiler in another process in linux, so it continues to run on its own after terminating ghci (calculating the infinite list)

  • @VladTrishch
    @VladTrishch 4 года назад +11

    Thanks a lot for making these videos, Philipp. They really help me to become a better FP developer.

  • @inigo8740
    @inigo8740 4 года назад +8

    I came up with a different way of getting the natural numbers:
    nat = 0 : map (+1) nat
    Is there a disadvantage to using this way? Does it have a greater time complexity?

    • @philipphagenlocher
      @philipphagenlocher  4 года назад +4

      That's really neat, actually!
      I don't think there is any downside to this definition. The time complexity should be the same, since every new term built by the map function is lazily evaluated. The thunks created might be a tiny bit bigger (I'm not sure though).

    • @enderger5308
      @enderger5308 3 года назад +3

      A slight improvement may be:
      nats = 0 : map succ nats

    • @gabrielwilliams6373
      @gabrielwilliams6373 2 года назад

      @@philipphagenlocher, how about:
      nats = [1,2..]
      evenNats = [2,4..]
      oddNats = [1,3..]

  • @MrMirville
    @MrMirville 3 года назад +4

    I have noticed that very many Haskell elementary functions and operators (second-level functions) do the very same jobs as those of the late great language APL, like map, like zipwith, like foldl which used to be noted / and scan which used to be noted backslash, like max and min which used to be elementary operations denoted by Greek letters. I am wondering whether like notations should be made admissible in Haskell. I for one would make any scalar-value-only functions mappable onto lists and arrays as long as no ambiguity arise, and also on any entity where fmap is defined. A point immediately after the scalar dyadic function would make it zip with the next entity : fibs = fibs +.(Drop fibs). I would use (+) // for foldl add and (+)\\ for scan.

    • @micknamens8659
      @micknamens8659 2 года назад +2

      -- regular haskell:
      fibs = 0:1:fibs +. tail fibs
      where (+.) = zipWith (+)

    • @myxail0
      @myxail0 Год назад

      yeah, as Mick said, just define your own operators

  • @_ia_2094
    @_ia_2094 3 года назад

    Yes! Thank you for the "WHY" ... the Application portion.

  • @GlimmerOfLight
    @GlimmerOfLight Год назад +1

    At 6:29 "has quadratic time complexity" is not correct: it's actually exponential (2**n) which is even worse ... a lot!

  • @pewpewpew8613
    @pewpewpew8613 4 года назад +2

    I am sorry but how can we use drop function with infinite list if we cannot evaluate end of the list?

    • @donovanvanderlinde3478
      @donovanvanderlinde3478 3 года назад

      We evaluate them one at a time
      SInce time isn't infinite real lists will terminate unless something is broken
      We evaluate them as we need them instead of all of them at once
      This actually solves the problem you are mentioning rather than creating them

  • @micknamens8659
    @micknamens8659 2 года назад +1

    -- 4:12 Using foldr for infinite lists is possible. But you have to use an irrefutable pattern (~) for the accumulator parameter:
    chunksOf n = foldr fn [[]] . zip [1..]
    where fn (i,x) ~(inner:outer)
    | i `mod` n == 0 = [x]:inner:outer
    | otherwise = (x:inner): outer
    segmentAfter p = foldr fn [[]]
    where fn x ~(inner:outer)
    | p x = [x]:inner:outer
    | otherwise = (x:inner): outer

  • @smunxgforce7889
    @smunxgforce7889 3 года назад

    A simple implementation of Fibonacci
    fib = t 0 1 where t n1 n2 = n1: (t n2 $ n1 + n2)

  • @johnsoncheung2113
    @johnsoncheung2113 4 года назад

    How Haskell calling window com object like Excel? Here is example of odbc & SQL client.
    sirocco.hatenadiary.org/entry/20100418/1271569324
    This example seems using package *hcom*

  • @DrMaxPlank
    @DrMaxPlank 4 года назад +1

    all about infinity is only math construct what is not in the real life, we have only big or small numbers. we should consider time is also not divided in the infinite small time parts, knowns as planck time, the planck time is the smallest amount of time