scale in alluminio professionali

Archivio mensile:agosto 2009

I assume you know what are foldl and foldr and have basic experience with them – you know how to express functions like sum, maximum using these folds. Here are some very nice diagrams showing fold and related functions.

AutomataA finite automaton is some device that has finitely many states (finite memory), reads a string, letter by letter, and changes its state according to some fixed transition rules. For example, it can remember if the number of a’s was even or odd – it toggles a single bit after each ‘a’. Here’s an automaton that checks if the whole word is built of ‘x’ and ‘y’ only:

aut = foldl (\state a -> state && (a == ‘x’ || a == ‘y’)) True

Automata are really doing foldl. In this case we could have written this more easily using all, but in general the behaviour can be quite complex.

Exercise:

I described a deterministic automaton – it always worked in the same way.

deterministic :: (a -> b -> a) -> a -> [b] -> adeterministic = foldl

Now, we have a nondeterministic one – it can choose from some list of possible states and go on. Return all possible states the automaton could finish with.

nondeterministic :: (a -> b -> [a]) -> a -> [b] -> [a]

Don’t worry about removing duplicates.

Solution:

You can regard a nondeterministic automaton as a deterministic one whose states are lists of states.

Does the convert function remind you of something? We can easily change the code to work with Maybe – the automaton will be deterministic, but it will be able to abort the computation anytime. It could also ask the user about the next state using IO. It’s the list monad.

b has changed into 1 + a + a a + a a a + … This is the same as a list, [a].

Equality of types x=y means that we can pair corresponding x’s and y’s. But we don’t need that. Instead, we just need a map b -> 1 + a b that doesn’t have to be an isomorphism. We can change all “equals” signs in above reasoning to ->:

b -> 1 + ab -> 1 + a(1 + ab) -> … -> 1 + a + aa + … -> [a]

and it stays OK. So, given a function (b -> 1 + a b), we get b -> [a]. This is known as unfolding. This name is very suggestive – we unfolded b into a list of a’s. Since 1 + a is called in Haskell Maybe a, we have a higher order function

unfold :: (b -> Maybe (a, b)) -> (b -> [a])

Exercise: Write it.

Solution:

unfold f x = case f x of Nothing -> [] Just (a,x') -> a:(unfold f x')

Data.List calls this function unfoldr.

If you invert this reasoning, given a function (f :: 1 + a b -> b) you get [a] -> b. This is folding. f Nothing is our initial value, and f $ Just (a,b) tells how to combine a and b.

Fold vs unfoldSome simple exercises. Recursion is not allowed directly, use only foldl, foldr and unfold.

1. Write factorial.

2. Write functions

toBinary :: Integer -> [Bool]fromBinary :: [Bool] -> Integer

that convert between a natural number and its binary representation.For example, fromBinary [False, True, True] == 6.