Haskell for Imperative Programmers #19 - Infinite Lists

แชร์
ฝัง

ความคิดเห็น • 22

  • @philipphagenlocher
    @philipphagenlocher  4 ปีที่แล้ว +17

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

    • @georgmaerz5599
      @georgmaerz5599 2 ปีที่แล้ว

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

  • @yanhuang5494
    @yanhuang5494 3 ปีที่แล้ว +23

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

    • @NaderHGhanbari
      @NaderHGhanbari 3 ปีที่แล้ว +4

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

    • @NewYorkFreeman
      @NewYorkFreeman 7 หลายเดือนก่อน +1

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

  • @VladTrishch
    @VladTrishch 4 ปีที่แล้ว +11

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

  • @_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!

  • @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

  • @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..]

  • @pewpewpew8613
    @pewpewpew8613 3 ปีที่แล้ว +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

  • @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*

  • @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)

  • @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