-
Notifications
You must be signed in to change notification settings - Fork 367
Documentation: Sequences #791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3c0109b
Inception of a document about Seq
7191b2c
Apply suggestions from code review
cuihtlauac 536da27
Add sieve of Eratosthenes example
8d42fac
Update data/tutorials/ds_05_seq.md
cuihtlauac 3f38371
Add unfold and conversion sections
eab0ecb
Cleanup
1c4c726
Add corecursion link
21deb5f
Apply suggestions from code review
cuihtlauac f1f21b0
Add fibs example
bc3fc64
Fix Typo
d6004bb
Add exercises
7b48116
Apply suggestions from code review
cuihtlauac 2021ef4
Apply suggestions from code review
cuihtlauac 32a1eda
Some fixes
bebd94a
Update text after letting it rest for a while
1351dd0
Add feedback from @gpetiot
5ef58f1
More feedback from @gpetiot
22be132
Add feedback from @xvw
32b61d9
Apply suggestions from @christinerose
cuihtlauac cca2dde
Update ds_05_seq.md
cuihtlauac 5305db6
Some more edits
800df7c
Update data/tutorials/ds_05_seq.md
cuihtlauac cc0fa39
Add feedback from Simon Cruanes
305be61
Add suggestions from Christine Rose
4d41d72
Add missing coma
d52fbdb
Fix code samples
962f070
Fix code samples
a72d569
Add Credits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| title: Diagonal of a Sequence of Sequences | ||
| number: "101" | ||
| difficulty: intermediate | ||
| tags: [ "seq" ] | ||
| --- | ||
|
|
||
| # Solution | ||
|
|
||
| ```ocaml | ||
| let rec diag seq_seq () = | ||
| let hds, tls = Seq.filter_map Seq.uncons seq_seq |> Seq.split in | ||
| let hd, tl = Seq.uncons hds |> Option.map fst, Seq.uncons tls |> Option.map snd in | ||
| let d = Option.fold ~none:Seq.empty ~some:diag tl in | ||
| Option.fold ~none:Fun.id ~some:Seq.cons hd d () | ||
| ``` | ||
|
|
||
| # Statement | ||
|
|
||
| Write a function `diag : 'a Seq.t Seq.t -> 'a Seq` that returns the _diagonal_ | ||
| of a sequence of sequences. The returned sequence is formed as follows: | ||
| The first element of the returned sequence is the first element of the first | ||
| sequence; the second element of the returned sequence is the second element of | ||
| the second sequence; the third element of the returned sequence is the third | ||
| element of the third sequence; and so on. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --- | ||
| title: Never-Ending Sequences | ||
| number: "100" | ||
| difficulty: beginner | ||
| tags: [ "seq" ] | ||
| --- | ||
|
|
||
| # Solution | ||
|
|
||
| ```ocaml | ||
| type 'a cons = Cons of 'a * 'a stream | ||
| and 'a stream = unit -> 'a cons | ||
|
|
||
| let hd (seq : 'a stream) = let (Cons (x, _)) = seq () in x | ||
| let tl (seq : 'a stream) = let (Cons (_, seq)) = seq () in seq | ||
| let rec take n seq = if n = 0 then [] else let (Cons (x, seq)) = seq () in x :: take (n - 1) seq | ||
| let rec unfold f x () = let (y, x) = f x in Cons (y, unfold f x) | ||
| let bang x = unfold (fun x -> (x, x)) x | ||
| let ints x = unfold (fun x -> (x, x + 1)) x | ||
| let rec map f seq () = let (Cons (x, seq)) = seq () in Cons (f x, map f seq) | ||
| let rec filter p seq () = let (Cons (x, seq)) = seq () in let seq = filter p seq in if p x then Cons (x, seq) else seq () | ||
| let rec iter f seq = let (Cons (x, seq)) = seq () in f x; iter f seq | ||
| let to_seq seq = Seq.unfold (fun seq -> Some (hd seq, tl seq)) seq | ||
| let rec of_seq seq () = match seq () with | ||
| | Seq.Nil -> failwith "Not a infinite sequence" | ||
| | Seq.Cons (x, seq) -> Cons (x, of_seq seq) | ||
| ``` | ||
|
|
||
| # Statement | ||
|
|
||
| Lists are finite, meaning they always contain a finite number of elements. Sequences may | ||
| be finite or infinite. | ||
|
|
||
| The goal of this exercise is to define a type `'a stream` which only contains | ||
| infinite sequences. Using this type, define the following functions: | ||
| ```ocaml | ||
| val hd : 'a stream -> 'a | ||
| (** Returns the first element of a stream *) | ||
| val tl : 'a stream -> 'a stream | ||
| (** Removes the first element of a stream *) | ||
| val take : int -> 'a stream -> 'a list | ||
| (** [take n seq] returns the n first values of [seq] *) | ||
| val unfold : ('a -> 'b * 'a) -> 'a -> 'b stream | ||
| (** Similar to Seq.unfold *) | ||
| val bang : 'a -> 'a stream | ||
| (** [bang x] produces an infinitely repeating sequence of [x] values. *) | ||
| val ints : int -> int stream | ||
| (* Similar to Seq.ints *) | ||
| val map : ('a -> 'b) -> 'a stream -> 'b stream | ||
| (** Similar to List.map and Seq.map *) | ||
| val filter: ('a -> bool) -> 'a stream -> 'a stream | ||
| (** Similar to List.filter and Seq.filter *) | ||
| val iter : ('a -> unit) -> 'a stream -> 'b | ||
| (** Similar to List.iter and Seq.iter *) | ||
| val to_seq : 'a stream -> 'a Seq.t | ||
| (** Translates an ['a stream] into an ['a Seq.t] *) | ||
| val of_seq : 'a Seq.t -> 'a stream | ||
| (** Translates an ['a Seq.t] into an ['a stream] | ||
| @raise Failure if the input sequence is finite. *) | ||
| ``` | ||
| **Tip:** Use `let ... =` patterns. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.