Changeset 26467 in project
 Timestamp:
 04/13/12 03:46:49 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

wiki/eggref/4/monad
r26466 r26467 1 * mode: orgmode; * 2 3 * Monads 1 == Monads 4 2 5 3 Monads for Scheme … … 8 6 dan@ironoxide.ca 9 7 10 * Description 8 [[toc:]] 11 9 12 ** What is a Monad? 10 == Description 11 12 === What is a Monad? 13 13 14 14 Monads are like a burrito. … … 32 32 The Monad egg allows you to write that code once and remain focused on the task at hand. 33 33 34 **Why not use miscmacros:doto instead?34 === Why not use miscmacros:doto instead? 35 35 36 36 Because you may want intermediary control and multiple usage of a unit function. … … 40 40 Yes, sometimes this isn't strictly a necessary set of functionality. But for when it is, save yourself some time and write a monad. 41 41 42 ** What's this about lazy evaluation? 42 === Why not use [[Foperator]] instead? 43 44 If you can understand what that is or how it works and it suits your needs better than this egg, then please, go right ahead. 45 46 === What's this about lazy evaluation? 43 47 44 48 Yes, this monad implementation is lazily evaluated. 45 46 That is, the monads are.47 49 48 50 When (run) is called with a given Monad the bind and unit functions are evaluated and their actions are undertaken. This happens once, and once only. Further executions of (run) will simply return the computed value of the first execution. … … 52 54 53 55 For example: 56 <enscript highlight="scheme"> 54 57 #;1> (use monad) 55 58 ; loading /usr/local/lib/chicken/6/monad.so ... … … 69 72 #<id> 2 70 73 # 74 </enscript> 71 75 72 *Functions76 == Functions 73 77 74 78 <procedure>(definemonad name unitfunction bindfunction)</procedure> … … 92 96 93 97 Ie: 94 98 <enscript highlight="scheme"> 95 99 (using <id> 96 100 (>>= (return 1) (lambda (x) (+ x 1)))) 97 101 98 Is the same as:102 ;Is the same as: 99 103 100 104 (<id>bind (<id>unit 1) (lambda (x) (+ x 1))) 105 </enscript> 101 106 102 107 <procedure>(dotousing monad init [body ...])</procedure> … … 107 112 108 113 Ie, 109 114 <enscript highlight="scheme"> 110 115 (dotousing <id> (+ 0 1) 111 116 (lambda (x) (+ x 1)) 112 117 (lambda (y) (+ y 2))) 113 118 114 Is the same as:119 ;Is the same as: 115 120 116 121 (using <id> … … 118 123 (lambda (x) (+ x 1))) 119 124 (lambda (y) (+ y 2)))) 125 </enscript> 120 126 121 **Basic Monads127 == Basic Monads 122 128 123 *** Identity 129 These monads are all predefined by this egg. 124 130 131 === Identity 132 133 <enscript highlight="scheme"> 125 134 (definemonad 126 135 <id> 127 136 (lambda (a) a) 128 137 (lambda (a f) (f a))) 138 </enscript> 129 139 130 ***Maybe140 === Maybe 131 141 142 <enscript highlight="scheme"> 132 143 (definemonad 133 144 <maybe> 134 145 (lambda (a) a) 135 146 (lambda (a f) (if a (f a) #f))) 147 </enscript> 136 148 137 ***List149 === List 138 150 151 <enscript highlight="scheme"> 139 152 (definemonad 140 153 <list> 141 154 (lambda (a) (list a)) 142 155 (lambda (a f) (concatenate! (map! f a)))) 156 </enscript> 143 157 144 **Contribution158 == Contribution 145 159 146 160 Contributions are welcome provided you accept the license I have chosen for this egg for the contributions themselves. 147 161 148 The github repository is at: 149 https://github.com/dleslie/monadegg 162 The github repository is at: [[https://github.com/dleslie/monadegg]] 150 163 151 *License164 = License 152 165 153 166 Copyright 2012 Daniel J. Leslie. All rights reserved.
Note: See TracChangeset
for help on using the changeset viewer.