mirror of
https://github.com/bitemyapp/learnhaskell.git
synced 2024-11-17 15:25:45 +00:00
Inverse of a bijective function programmatically
This commit is contained in:
parent
3044bb2b85
commit
1e534f2fa8
17
dialogues.md
17
dialogues.md
@ -1176,3 +1176,20 @@ g xs = map last $ groupBy (<) xs
|
|||||||
λ> map last $ groupBy (<) [1, 2, 1, 2, 3, 1, 2, 3, 4, 5, 6, 1, 1, 2, 3, 4]
|
λ> map last $ groupBy (<) [1, 2, 1, 2, 3, 1, 2, 3, 4, 5, 6, 1, 1, 2, 3, 4]
|
||||||
[2,3,6,1,4]
|
[2,3,6,1,4]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Programmatically finding the inverse of a bijective function
|
||||||
|
|
||||||
|
tel:
|
||||||
|
|
||||||
|
If the range is finite and the domain has decidable equality
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
class Finite a where
|
||||||
|
-- x :: a iff elem x universe = True
|
||||||
|
universe :: [a]
|
||||||
|
|
||||||
|
inv :: (Eq b, Finite a) => (a -> b) -> (b -> a)
|
||||||
|
inv f b = let [a] = [ a | a <- universe, f a == b ] in a
|
||||||
|
```
|
||||||
|
|
||||||
|
The pattern matching above will fail if f is not bijective.
|
||||||
|
Loading…
Reference in New Issue
Block a user