windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Given these type declarations in OCaml for a stream type, how to I write a flatten function?

Asked By: Anonymous
exception Empty_list

type 'a stream = Stream of 'a * (unit -> 'a stream) | Nil

let shead s = match s with
  | Stream (a, _) -> a
  | Nil -> raise Empty_list

let stail s = match s with
  | Stream (_, s) -> s ()
  | Nil -> raise Empty_list

let stream_to_list s =
  let rec stream_to_list s accumulator = match s with
    | Nil -> accumulator
    | Stream (_, _) -> stream_to_list (stail s) (shead s :: accumulator)
  in List.rev (stream_to_list s [])

The flatten function would take an arbitrarily nested set of streams and produce a completely flattened version that’s a result of an inorder traversal of the nesting structure and contains only the leaves. This would mean that the result of shead on each member of this new thing would return something that was not a stream.

The idea is that stream_to_list (flatten s) would give back a list in which no element was a stream.


Solution

Answered By: Anonymous

Your desired function is ill-typed and cannot exist in a language with parametric polymorphism like OCaml: 'a stream -> 'b stream where 'b is not a stream is not a valid type.

Parametric polymorphism requires that polymorphic functions does not change their behavior in function of the type of their argument. This useful both in term of semantics, types can be erased once type checking is done, and for type theoretical reason: proofs (aka the program) cannot inspect the theorem (aka the type) that they are trying to prove.

If you want to flatten multiple times a nested streams they are two options:

  • if the nested level is known statically, you can use flatten multiple times. Note that it is straightforward to flatten a number of time exponential with the size of the code
let flatten16 x =
  let flatten2 x = flatten (flatten x) in 
  let flatten4 x = flatten2 (flatten2 x) in 
  let flatten8 x = flatten4 (flatten4 x) in 
  flatten8 (flatten8 x)

This is completely sufficient in practice since types don’t grow exponentially in human written code.

  • If the level of nesting is arbitrary, the level of nesting needs to be visible on the value level (which is already needed to construct the stream). This can be achieved with the following variant type:
type 'a nested_seq = Seq of 'a Seq.t | Nested of 'a Seq.t nested_seq

(Your type is equivalent to Seq.t from the standard library)

Then flattening a 'a nested_seq to a 'a Seq.t is a well-defined notion and that can be done with some polymorphic recursion:

let rec flatten: 'a. 'a nested_seq -> 'a Seq.t = fun x ->
  match x with
  | Seq x -> x
  | Nested s -> Seq.flat_map Fun.id (flatten s) 
  (* or `Seq.concat (flatten s)` in OCaml 4.13 or with a sequence libray  *)

Aside: shead and stail are anti-patterns: in order to safely use those functions, you need to pattern match their future argument, and them discard the matched value in each branches.

In other words,

let head s =
  match s with
  | Nil -> None
  | Stream _ -> Some (shead s)

is both less clear and less safe than

let head s =
  match s with
  | Nil -> None
  | Stream (s, _) -> Some s

(The option functions are useful to compose with generic option functions (from the option monad for instance)).

techinplanet staff

Porno Gratuit Porno Français Adulte XXX Brazzers Porn College Girls Film érotique Hard Porn Inceste Famille Porno Japonais Asiatique Jeunes Filles Porno Latin Brown Femmes Porn Mobile Porn Russe Porn Stars Porno Arabe Turc Porno caché Porno de qualité HD Porno Gratuit Porno Mature de Milf Porno Noir Regarder Porn Relations Lesbiennes Secrétaire de Bureau Porn Sexe en Groupe Sexe Gay Sexe Oral Vidéo Amateur Vidéo Anal

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno

adana escort adapazarı escort adıyaman escort afşin escort afyon escort ağrı escort ahlat escort akçaabat escort akçakale escort akçakoca escort akdeniz escort akhisar escort aksaray escort akşehir escort aksu escort akyazı escort alanya escort alaşehir escort aliağa escort altıeylül escort altınordu escort amasya escort anamur escort ankara escort antakya escort antalya escort araklı escort ardahan escort ardeşen escort arnavutköy escort atakum escort avanos escort avcılar escort aydın escort ayvacık escort ayvalık escort aziziye escort babaeski escort bafra escort bağcılar escort bağlar escort balıkesir escort bandırma escort bartın escort batıkent escort batman escort battalgazi escort bayburt escort belek escort besni escort beylikdüzü escort beyşehir escort biga escort bilecik escort bingöl escort birecik escort bismil escort bitlis escort bodrum escort bolu escort bor escort bornova escort boyabat escort bozüyük escort buca escort bucak escort bulancak escort bulanık escort bünyan escort burdur escort bursa escort çan escort çanakkale escort canik escort çankaya escort çankırı escort çarşamba escort çayeli escort çekmeköy escort çerkeş escort çerkezköy escort çeşme escort ceyhan escort çivril escort çorlu escort çorum escort çubuk escort çukurova escort çumra escort dalaman escort darıca escort datça escort davutlar escort defne escort denizli escort derince escort develi escort didim escort digor escort dinar escort diyadin escort diyarbakır escort doğanşehir escort doğubayazıt escort dörtyol escort dulkadiroğlu escort düzce escort edirne escort edremit escort efeler escort elazığ escort elbistan escort emirdağ escort erbaa escort erdemli escort ereğli escort erenler escort ergani escort ergene escort erzincan escort erzurum escort escort esenler escort eskil escort eskişehir escort espiye escort eyyübiye escort fatih escort fatsa escort fethiye escort finike escort fındıklı escort gaziantep escort gaziemir escort gebze escort gediz escort gelibolu escort gemerek escort gemlik escort gerede escort giresun escort gölbaşı escort gölcük escort görele escort görükle escort göynük escort gümbet escort gümüşhane escort güngören escort güroymak escort gürsu escort hakkari escort haliliye escort hatay escort haymana escort horasan escort iğdır escort ilgaz escort i̇lkadım escort i̇negöl escort i̇skenderun escort isparta escort i̇stanbul escort i̇zmir escort i̇zmit escort i̇znik escort kadirli escort kadıköy escort kağıthane escort kağızman escort kahta escort kaman escort kangal escort kapaklı escort karabük escort karacabey escort karahayıt escort karaköprü escort karaman escort karatay escort karesi escort kars escort karşıyaka escort kartal escort kaş escort kastamonu escort kaynaşlı escort kayseri escort keçiören escort kemer escort kepez escort keşan escort keskin escort kestel escort kilis escort kırıkhan escort kırıkkale escort kırkağaç escort kırklareli escort kırşehir escort kızıltepe escort kocaeli escort kocasinan escort konak escort konya escort konyaaltı escort körfez escort korkuteli escort köyceğiz escort kozan escort kumluca escort kumru escort kuşadası escort kütahya escort lara escort lüleburgaz escort malatya escort malazgirt escort malkara escort maltepe escort mamak escort manavgat escort manisa escort maraş escort mardin escort marmaris escort melikgazi escort menemen escort menteşe escort meram escort mersin escort merzifon escort mezitli escort milas escort mucur escort mudanya escort mudurnu escort muğla escort muratpaşa escort muş escort mut escort nazilli escort nevşehir escort niğde escort niksar escort nilüfer escort nizip escort nurdağı escort ödemiş escort of escort oltu escort onikişubat escort ordu escort orhangazi escort ortahisar escort ortaköy escort osmancık escort osmangazi escort osmaniye escort palandöken escort pamukkale escort pamukova escort patnos escort payas escort pazar escort pazarcık escort pendik escort polatlı escort rize escort safranbolu escort şahinbey escort sakarya escort salihli escort samandağ escort samsun escort sandıklı escort şanlıurfa escort sapanca escort sarıçam escort sarıkamış escort sarıyer escort şarkışla escort şehitkamil escort selçuklu escort selim escort serdivan escort serik escort seyhan escort siirt escort silifke escort simav escort sincan escort sinop escort şişli escort sivas escort siverek escort şırnak escort söğüt escort söke escort soma escort sultangazi escort suluova escort sungurlu escort sur escort suruç escort suşehri escort susurluk escort talas escort tarsus escort taşova escort tavşanlı escort tekirdağ escort terme escort tire escort tirebolu escort tokat escort torbalı escort toroslar escort trabzon escort turgutlu escort turhal escort türkoğlu escort tuzla escort ulus escort ümraniye escort ünye escort ürgüp escort urla escort uşak escort üsküdar escort üzümlü escort van escort varto escort viranşehir escort yahşihan escort yahyalı escort yakutiye escort yalova escort yatağan escort yenişehir escort yeşilyurt escort yıldırım escort yıldızeli escort yomra escort yozgat escort yunusemre escort yüreğir escort zara escort zeytinburnu escort zile escort zonguldak escort mersin escort aydın escort manisa escort muğla escort tekirdağ escort urfa escort yalova escort fethiye escort