-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions in vmchale/recursion_schemes#5
- Loading branch information
Showing
9 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
# dropWhile | ||
|
||
```hs | ||
module Algorithms.List.BasicOperations.DropWhile where | ||
|
||
import Data.Functor.Foldable | ||
|
||
import RecursionSchemes.Extra | ||
``` | ||
|
||
You can implement `dropWhile` by using paramorphism and returning the list of the points where the element no longer satisfies the condition. | ||
|
||
```hs | ||
-- | >>> dropWhilePara odd [3, 1, 4, 1, 5] | ||
-- [4,1,5] | ||
dropWhilePara :: (a -> Bool) -> [a] -> [a] | ||
dropWhilePara p = para \case | ||
Nil -> [] | ||
Cons x (xs, ys) -> if p x then ys else x:xs | ||
``` | ||
|
||
Monadic dropWhile also uses paramorphism, but the difference is that the object to be folded is wrapped in the monad[^1]. | ||
|
||
```hs | ||
-- | >>> dropWhileParaM (\i -> print i >> pure (odd i)) [3, 1, 4, 1, 5] | ||
-- 3 | ||
-- 1 | ||
-- 4 | ||
-- [4,1,5] | ||
dropWhileParaM :: Monad m => (a -> m Bool) -> [a] -> m [a] | ||
dropWhileParaM p = para \case | ||
Nil -> pure [] | ||
Cons x (xs, ys) -> do | ||
flg <- p x | ||
if flg then ys else pure (x:xs) | ||
``` | ||
|
||
## References | ||
[1] [Monadic versions · Issue #5 · vmchale/recursion_schemes](https://github.com/vmchale/recursion_schemes/issues/5) |
This file contains 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
This file contains 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,19 @@ | ||
# inits | ||
|
||
```hs | ||
module Algorithms.List.BasicOperations.Inits where | ||
|
||
import Algorithms.List.BasicOperations.TakeWhile | ||
``` | ||
|
||
`inits` is a function that returns all substrings of the list in succession from the beginning[^1]. | ||
|
||
```hs | ||
-- | >>> inits "abc" | ||
-- ["","a","ab","abc"] | ||
inits :: [a] -> [[a]] | ||
inits = takeWhileCataM (const [False, True]) | ||
``` | ||
|
||
## References | ||
[1] [Monadic versions · Issue #5 · vmchale/recursion_schemes](https://github.com/vmchale/recursion_schemes/issues/5) |
This file contains 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,19 @@ | ||
# subsequences | ||
|
||
```hs | ||
module Algorithms.List.BasicOperations.Subsequences where | ||
|
||
import Algorithms.List.BasicOperations.Filter | ||
``` | ||
|
||
`subsequences` is a function that returns a partial list of all parts of a given list[^1]. | ||
|
||
```hs | ||
-- | >>> subsequences "abc" | ||
-- ["","a","b","ab","c","ac","bc","abc"] | ||
subsequences :: [a] -> [[a]] | ||
subsequences = filterCataM (const [False, True]) | ||
``` | ||
|
||
## References | ||
[1] [Monadic versions · Issue #5 · vmchale/recursion_schemes](https://github.com/vmchale/recursion_schemes/issues/5) |
This file contains 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,19 @@ | ||
# tails | ||
|
||
```hs | ||
module Algorithms.List.BasicOperations.Tails where | ||
|
||
import Algorithms.List.BasicOperations.DropWhile | ||
``` | ||
|
||
`inits` is a function that returns all substrings of the list in succession from the beginning[^1]. | ||
|
||
```hs | ||
-- | >>> tails "abc" | ||
-- ["abc","bc","c",""] | ||
tails :: [a] -> [[a]] | ||
tails = dropWhileParaM (const [False, True]) | ||
``` | ||
|
||
## References | ||
[1] [Monadic versions · Issue #5 · vmchale/recursion_schemes](https://github.com/vmchale/recursion_schemes/issues/5) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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