Changeset 26467 in project


Ignore:
Timestamp:
04/13/12 03:46:49 (8 years ago)
Author:
svnwiki
Message:

Anonymous wiki edit for IP [70.71.187.169]: Converted syntax

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/monad

    r26466 r26467  
    1 -*- mode: org-mode; -*-
    2 
    3 * Monads
     1== Monads
    42
    53Monads for Scheme
     
    86dan@ironoxide.ca
    97
    10 * Description
     8[[toc:]]
    119
    12 ** What is a Monad?
     10== Description
     11
     12=== What is a Monad?
    1313
    1414Monads are like a burrito.
     
    3232The Monad egg allows you to write that code once and remain focused on the task at hand.
    3333
    34 ** Why not use miscmacros:doto instead?
     34=== Why not use miscmacros:doto instead?
    3535
    3636Because you may want intermediary control and multiple usage of a unit function.
     
    4040Yes, sometimes this isn't strictly a necessary set of functionality. But for when it is, save yourself some time and write a monad.
    4141
    42 ** What's this about lazy evaluation?
     42=== Why not use [[F-operator]] instead?
     43
     44If 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?
    4347
    4448Yes, this monad implementation is lazily evaluated.
    45 
    46 That is, the monads are.
    4749
    4850When (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.
     
    5254
    5355For example:
     56<enscript highlight="scheme">
    5457#;1> (use monad)
    5558; loading /usr/local/lib/chicken/6/monad.so ...
     
    6972#<id> 2
    7073#
     74</enscript>
    7175
    72 * Functions
     76== Functions
    7377
    7478<procedure>(define-monad name unit-function bind-function)</procedure>
     
    9296
    9397Ie:
    94 
     98<enscript highlight="scheme">
    9599(using <id>
    96100  (>>= (return 1) (lambda (x) (+ x 1))))
    97101
    98 Is the same as:
     102;Is the same as:
    99103
    100104(<id>-bind (<id>-unit 1) (lambda  (x) (+ x 1)))
     105</enscript>
    101106
    102107<procedure>(doto-using monad init [body ...])</procedure>
     
    107112
    108113Ie,
    109 
     114<enscript highlight="scheme">
    110115(doto-using <id> (+ 0 1)
    111116  (lambda (x) (+ x 1))
    112117  (lambda (y) (+ y 2)))
    113118
    114 Is the same as:
     119;Is the same as:
    115120
    116121(using <id>
     
    118123            (lambda (x) (+ x 1)))
    119124       (lambda (y) (+ y 2))))
     125</enscript>
    120126
    121 ** Basic Monads
     127== Basic Monads
    122128
    123 *** Identity
     129These monads are all pre-defined by this egg.
    124130
     131=== Identity
     132
     133<enscript highlight="scheme">
    125134 (define-monad
    126135   <id>
    127136   (lambda (a) a)
    128137   (lambda (a f) (f a)))
     138</enscript>
    129139
    130 *** Maybe
     140=== Maybe
    131141
     142<enscript highlight="scheme">
    132143 (define-monad
    133144   <maybe>
    134145   (lambda (a) a)
    135146   (lambda (a f) (if a (f a) #f)))
     147</enscript>
    136148
    137 *** List
     149=== List
    138150
     151<enscript highlight="scheme">
    139152 (define-monad
    140153   <list>
    141154   (lambda (a) (list a))
    142155   (lambda (a f) (concatenate! (map! f a))))
     156</enscript>
    143157
    144 ** Contribution
     158== Contribution
    145159
    146160Contributions are welcome provided you accept the license I have chosen for this egg for the contributions themselves.
    147161
    148 The github repository is at:
    149 https://github.com/dleslie/monad-egg
     162The github repository is at: [[https://github.com/dleslie/monad-egg]]
    150163
    151 * License
     164= License
    152165
    153166Copyright 2012 Daniel J. Leslie. All rights reserved.
Note: See TracChangeset for help on using the changeset viewer.