Differences

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

monads [2018/04/25 23:39]
mvc
monads [2018/11/16 09:55] (current)
mvc
Line 1: Line 1:
-====== Assignment 13: Introduction to Monads ======+====== Assignment 12: Introduction to Monads ======
<!-- <!--
<blockquote James Iry, attributed to Phil Wadler > <blockquote James Iry, attributed to Phil Wadler >
Line 28: Line 28:
In order to receive credit: In order to receive credit:
-  * You should use the {{http://pages.iu.edu/~mvc/c311/monads.rkt|monads.rkt}} file. You should add it to your C311 directory. This contains all the monad definitions you should need. +  * You should use the {{https://github.com/mvcccccc/C311Pub/blob/master/monads.rkt|monads.rkt}} file. You should add it to your C311 directory. This contains all the monad definitions you should need.
  * You should also pull down and use the {{http://pages.iu.edu/~mvc/c311/monads-student-tests.rkt|monads-student-tests.rkt}} test file.   * You should also pull down and use the {{http://pages.iu.edu/~mvc/c311/monads-student-tests.rkt|monads-student-tests.rkt}} test file.
  * Use monadic style.   * Use monadic style.
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>
Line 98: Line 98:
===== State Monad ===== ===== State Monad =====
-Recall from lecture that the ''state'' monad uses a state and works with ''return-state'' and ''bind-state''.+Recall from lecture that the ''state'' monad uses a state and works with ''inj-state'' and ''bind-state''.
** 4. ** Given a symbol ''x'' and a binary tree of symbols ''tr'' (i.e. a tree with symbols at the leaves), via a preorder walk replace every occurrence of ''x'' with the number of ''x''s that have been seen so far. ** 4. ** Given a symbol ''x'' and a binary tree of symbols ''tr'' (i.e. a tree with symbols at the leaves), via a preorder walk replace every occurrence of ''x'' with the number of ''x''s that have been seen so far.
Line 107: Line 107:
> ((run-state (replace-with-count 'o '(((h (i s . o) . a) o s o e . n) . m))) 0) > ((run-state (replace-with-count 'o '(((h (i s . o) . a) o s o e . n) . m))) 0)
-'3 ((h (i s . 0) . a) 1 s 2 e . n) . m))+(3 . ((h (i s . 0) . a) 1 s 2 e . n) . m))
> ((run-state (replace-with-count 'o '(o (h (o s . o) . o) . o))) 1) > ((run-state (replace-with-count 'o '(o (h (o s . o) . o) . o))) 1)
Line 128: Line 128:
            (cond             (cond
              [(pair? tree)               [(pair? tree)
-                (go-on +                (go-on ([a (trav (car tree))] 
-                  ([a (trav (car tree))] +                       [d (trav (cdr tree))])
-                   [d (trav (cdr tree))])+
                  (inj (cons a d)))]                   (inj (cons a d)))]
              [else (f tree)]))))               [else (f tree)]))))
Line 186: Line 185:
** 7. ** State/sum. Implement a function ''state/sum'' which will, when given a number, return the current state as the value, and add that number to the current state. ** 7. ** State/sum. Implement a function ''state/sum'' which will, when given a number, return the current state as the value, and add that number to the current state.
 +Below, we give state/sum the number to sum the state with, then we call the result of ''run-state'' with the initial state. So in the first case, the state is 0 and the sum is 5.
<code lisp> <code lisp>
 

monads.1524713989.txt.gz · Last modified: 2018/04/25 23:39 by mvc