Differences

This shows you the differences between two versions of the page.

monads [2018/11/16 09:27]
mvc
monads [2018/11/16 09:55] (current)
mvc
Line 53: Line 53:
The ''writer'' monad provides a mechanism to write data separately from the actual return value. If we use a list to represent these writes, we can use this monad to implement some rather useful functions. The ''writer'' monad provides a mechanism to write data separately from the actual return value. If we use a list to represent these writes, we can use this monad to implement some rather useful functions.
-** 2. ** The function ''partition'' takes a list and a predicate, returning a dotted pair with the values that do not pass the predicate in the first position and the values that do in the second position. Implement this using the ''writer'' monad. //Do not use ''go-on'' syntax for this problem.//+** 2. ** The function ''partition'' takes a list and a predicate, returning a dotted pair with the values that do not pass the predicate in the writer part of the monad and the values that do in the value part. Implement this using the ''writer'' monad. //Do not use ''go-on'' syntax for this problem.//
<code lisp> <code lisp>
> (run-writer (partition-writer even? '(1 2 3 4 5 6 7 8 9 10))) > (run-writer (partition-writer even? '(1 2 3 4 5 6 7 8 9 10)))
-((2 4 6 8 10) . (1 3 5 7 9))+((1 3 5 7 9) . (2 4 6 8 10))
> (run-writer (partition-writer odd? '(1 2 3 4 5 6 7 8 9 10))) > (run-writer (partition-writer odd? '(1 2 3 4 5 6 7 8 9 10)))
-((1 3 5 7 9) . (2 4 6 8 10))+((2 4 6 8 10) . (1 3 5 7 9))
</code> </code>
 

monads.txt · Last modified: 2018/11/16 09:55 by mvc