|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Меню:
Главная
Форум
Литература: Программирование и ремонт Импульсные блоки питания Неисправности и замена Радиоэлектронная аппаратура Микросхема в ТА Рубрикатор ТА Кабельные линии Обмотки и изоляция Радиоаппаратура Гибкие диски часть 2 часть 3 часть 4 часть 5 Ремонт компьютера часть 2 Аналитика: Монтаж Справочник Электроника Мощные высокочастотные транзисторы 200 микросхем Полупроводники ч.1 Часть 2 Алгоритмические проблемы 500 микросхем 500 микросхем Сортировка и поиск Монады Передача сигнала Электроника Прием сигнала Телевидиние Проектирование Эвм Оптимизация Автомобильная электроника Поляковтрансиверы Форт Тензодатчик Силовые полевые транзисторы Распределение частот Резисторные и термопарные Оберон Открытые системы шифрования Удк |
[23] св а: =з It is clear that the worker machines contain all of the functionality needed to produce chopsticks. What is missing is the specification of the trays, loader, and combiner machines that collectively make up the Tray monad. Our trays should either be empty or contain a single item. Our loader machine would simply take an item and place it in a tray on the conveyor belt. The combiner machine would take each input tray and pass along empty trays while feeding the contents of non-empty trays to its worker machine. In Haskell, we would define the Tray monad as: -trays are either empty or contain a single item data Tray x = Empty Contains x -Tray is a monad instance Monad Tray where Empty>>= = Empty (Contains x) >>= worker = worker x return= Contains fail = Empty You may recognize the Tray monad as a disguised version of the Maybe monad that is a standard part of Haskell 98 library. All that remains is to sequence the worker machines together using the loader and combiner machines to make a complete assembly line, as shown in Figure A-2. Figure A-2. A complete assembly line for making chopsticks using a monadic approach. In Haskell, the sequencing can be done using the standard monadic functions: assemblyLine :: Wood -> Tray Wrapped Chopsticks assemblyLine w = (return w) >>= makeChopsticks >>= polishChopsticks >>= wrapChopsticks or using the built in Haskell "do" notation for monads: assemblyLine :: Wood -> Tray Wrapped Chopsticks assemblyLine w = do c <- makeChopsticks w c <- polishChopsticks c c <- wrapChopsticks c
£ со return c So far, you have seen how monads are like a framework for building assembly lines, but you probably havent been overawed by their utility. To see why we might want to build our assembly line using the monadic approach, consider what would happen if we wanted to change the manufacturing process. Right now, when a worker machine malfunctions, it uses the fail routine to produce an empty tray. The fail routine takes an argument describing the failure, but our Tray type ignores this and simply produces an empty tray. This empty tray travels down the assembly line and the combiner machines allow it to bypass the remaining worker machines. It eventually reaches the end of the assembly line, where it is brought to you, the quality control engineer. It is your job to figure out which machine failed, but all you have to go on is an empty tray. You realize that your job would be much easier if you took advantage of the failure messages that are currently ignored by the fail routine in your Tray monad. Because your assembly line is organized around a monadic approach, it is easy for you to add this functionality to your assembly line without changing your worker machines. To make the change, you simply create a new tray type that can never be empty. It will always either contain an item or it will contain a failure report describing the exact reason there is no item in the tray. - tray2s either contain a single item or contain a failure report data Tray2 x = Contains x Failed String - Tray2 is a monad instance Monad Tray2 where (Failed reason) >>= (Contains x) >>= worker fail reason Failed reason worker x Contains Failed reason You may recognize the Tray2 monad as a disguised version of the Error monad that is a standard part of the Haskell 98 libraries. Replacing the Tray monad with the Tray2 monad instantly upgrades your assembly line. Now when a failure occurs, the tray that is brought to the quality control engineer contains a failure report detailing the exact cause of the failure!
Приложение B (обязательное) HASKELL CODE EXAMPLES This appendix contains a list of all of the code examples supplied with the tutorial. Example 1 This example is discussed in the section: Meet the monads. The example code introduces the monad concept without using Haskell typeclasses. It shows how a monadic combinator can be used to simplify the construction of computations from sequences of computations which may not return a result. Example 2 This example is discussed in the section: Doing it with class. The example code builds on the first example, and shows how do-notation can be used with an instance of the Monad class (in this case, Maybe is the monad used). Example 3 This example is discussed in the section: Monad support in Haskell. The example code builds on the first two examples, and shows a somewhat atypical - but very powerful - use of the foldM function outside of a do-block. Example 4 This example is discussed in the section: Monad support in Haskell. The example code shows a more typical use of the foldM function within a do-block. It combines dictionary values read from different files into a single dictionary using the foldM function within the IO monad. Example 5 This example is discussed in the section: Monad support in Haskell.
|
Среды: Smalltalk80 MicroCap Local bus Bios Pci 12С ML Микроконтроллеры: Atmel Intel Holtek AVR MSP430 Microchip Книги: Емкостный датчик 500 схем для радиолюбителей часть 2 (4) Структура компьютерных программ Автоматическая коммутация Кондиционирование и вентиляция Ошибки при монтаже Схемы звуковоспроизведения Дроссели для питания Блоки питания Детекторы перемещения Теория электропривода Адаптивное управление Измерение параметров Печатная плата pcad pcb Физика цвета Управлении софтверными проектами Математический аппарат Битовые строки Микроконтроллер nios Команды управления выполнением программы Перехода от ahdl к vhdl Холодный спай Усилители hi-fi Электронные часы Сердечники из распылённого железа Анализ алгоритмов 8-разрядные КМОП Классификация МПК История Устройства автоматики Системы и сети Частотность Справочник микросхем Вторичного электропитания Типы видеомониторов Радиобиблиотека Электронные системы Бесконтекстный язык Управление техническими системами Монтаж печатных плат Работа с коммуникациями Создание библиотечного компонента Нейрокомпьютерная техника Parser Пи-регулятор ч.1 ПИ-регулятор ч.2 Обработка списков Интегральные схемы Шина ISAВ Шина PCI Прикладная криптография Нетематическое: Взрывной автогидролиз Нечеткая логика Бытовые установки (укр) Автоматизация проектирования Сбор и защита Дискретная математика Kb радиостанция Энергетика Ретро: Прием в автомобиле Управление шаговым двигателем Магнитная запись Ремонт микроволновки Дискретные системы часть 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||