From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.ada Subject: Re: Generators/coroutines in future Ada? Date: Wed, 12 Jul 2017 10:34:32 -0700 Organization: A noiseless patient Spider Message-ID: <87k23d4l07.fsf@nightsong.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="c3553a461fe13066e5e573bd233b3eaf"; logging-data="2647"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19gEE/JZ0+v3sYuL/MI3vHu" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:Iobq8WJdMEkdHWCLPAeaXDW4G2U= sha1:WxfHIsXpv97Z44P+MJpzbT6BZcc= Xref: news.eternal-september.org comp.lang.ada:47386 Date: 2017-07-12T10:34:32-07:00 List-Id: "J-P. Rosen" writes: > AFAIU, generators are functions with states, where the returned value at > one call depends on the state that was left behind by the previous call. > This can easily be achieved with a function within a package, where the > package serves to protect and hide the function's state. It's not always so easy though. Generators can yield from other generators, so doing them as functions with states can require the states to be stacks or collections of stacks. Doing it with tasks is more natural, but Ada tasks can be heavyweight, e.g. mapping to Posix threads. In Haskell, generators aren't even explicit: every list is automatically a generator. Here's an example which would be quite messy in Ada: a 5-smooth number a/k/a Hamming number is a number with no prime factors greater than 5. The first 20 of them are: 1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36 Problem: find the 1500th Hamming number (answer: 859963392). This is a classic Haskell problem solved by merging corecursively generated lists: hamming = 1 : m 2 `merge` m 3 `merge` m 5 where m k = map (k *) hamming xxs@(x:xs) `merge` yys@(y:ys) = case x `compare` y of LT -> x : (xs `merge` yys) EQ -> x : (xs `merge` ys) GT -> y : (xxs `merge` ys) main = print (hamming !! 1499) This prints the answer instantly (0.004 seconds elapsed, mostly in the operating system). I remember doing it in C++ and it was a page or so of ugly code.