CINXE.COM
Maybe - HaskellWiki
<!DOCTYPE html> <html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-zebra-design-disabled vector-feature-custom-font-size-clientpref-disabled vector-feature-client-preferences-disabled vector-feature-typography-survey-disabled vector-toc-available" lang="en" dir="ltr"> <head> <meta charset="UTF-8"> <title>Maybe - HaskellWiki</title> <script>(function(){var className="client-js vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-zebra-design-disabled vector-feature-custom-font-size-clientpref-disabled vector-feature-client-preferences-disabled vector-feature-typography-survey-disabled vector-toc-available";var cookie=document.cookie.match(/(?:^|; )mediawikimwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""], "wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"8cc1766355eaf4335df90b6d","wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"Maybe","wgTitle":"Maybe","wgCurRevisionId":66413,"wgRevisionId":66413,"wgArticleId":1598,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with syntax highlighting errors","Standard types","Monad"],"wgPageViewLanguage":"en","wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"Maybe","wgRelevantArticleId":1598,"wgIsProbablyEditable":false,"wgRelevantPageIsProbablyEditable":false,"wgRestrictionEdit":[],"wgRestrictionMove":[],"wgCollapsibleVectorEnabledModules":{"collapsiblenav":true,"experiments":true},"wgSmjUseCdn":true,"wgSmjUseChem":true,"wgSmjDisplayMath":[],"wgSmjExtraInlineMath":[], "wgSmjScale":1,"wgSmjEnableMenu":true,"wgSmjDisplayAlign":"left"};RLSTATE={"skins.vector.user.styles":"ready","site.styles":"ready","user.styles":"ready","skins.vector.user":"ready","user":"ready","user.options":"loading","ext.pygments":"ready","ext.math.styles":"ready","codex-search-styles":"ready","skins.vector.styles":"ready","skins.vector.icons":"ready"};RLPAGEMODULES=["ext.SimpleMathJax","site","mediawiki.page.ready","mediawiki.toc","skins.vector.js","ext.collapsiblevector.collapsibleNav"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/load.php?lang=en&modules=codex-search-styles%7Cext.math.styles%7Cext.pygments%7Cskins.vector.icons%2Cstyles&only=styles&skin=vector-2022"> <script async="" src="/load.php?lang=en&modules=startup&only=scripts&raw=1&skin=vector-2022"></script> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/load.php?lang=en&modules=site.styles&only=styles&skin=vector-2022"> <meta name="generator" content="MediaWiki 1.41.1"> <meta name="robots" content="max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=1000"> <link rel="icon" href="/wikistatic/favicon.ico"> <link rel="search" type="application/opensearchdescription+xml" href="/opensearch_desc.php" title="HaskellWiki (en)"> <link rel="EditURI" type="application/rsd+xml" href="https://wiki.haskell.org/api.php?action=rsd"> <link rel="license" href="/index.php?title=HaskellWiki:Copyrights"> <link rel="alternate" type="application/atom+xml" title="HaskellWiki Atom feed" href="/index.php?title=Special:RecentChanges&feed=atom"> </head> <body class="skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Maybe rootpage-Maybe skin-vector-2022 action-view"><a class="mw-jump-link" href="#bodyContent">Jump to content</a> <div class="vector-header-container"> <header class="vector-header mw-header"> <div class="vector-header-start"> <nav class="vector-main-menu-landmark" aria-label="Site" role="navigation"> <div id="vector-main-menu-dropdown" class="vector-dropdown vector-main-menu-dropdown vector-button-flush-left vector-button-flush-right" > <input type="checkbox" id="vector-main-menu-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-main-menu-dropdown" class="vector-dropdown-checkbox " aria-label="Main menu" > <label id="vector-main-menu-dropdown-label" for="vector-main-menu-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-menu mw-ui-icon-wikimedia-menu"></span> <span class="vector-dropdown-label-text">Main menu</span> </label> <div class="vector-dropdown-content"> <div id="vector-main-menu-unpinned-container" class="vector-unpinned-container"> <div id="vector-main-menu" class="vector-main-menu vector-pinnable-element"> <div class="vector-pinnable-header vector-main-menu-pinnable-header vector-pinnable-header-unpinned" data-feature-name="main-menu-pinned" data-pinnable-element-id="vector-main-menu" data-pinned-container-id="vector-main-menu-pinned-container" data-unpinned-container-id="vector-main-menu-unpinned-container" > <div class="vector-pinnable-header-label">Main menu</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-main-menu.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-main-menu.unpin">hide</button> </div> <div id="p-navigation" class="vector-menu mw-portlet mw-portlet-navigation" > <div class="vector-menu-heading"> Navigation </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-mainpage" class="mw-list-item"><a href="/index.php?title=Haskell" title="Visit the main page [z]" accesskey="z"><span>Haskell</span></a></li><li id="n-portal" class="mw-list-item"><a href="/index.php?title=HaskellWiki:Community" title="About the project, what you can do, where to find things"><span>Wiki community</span></a></li><li id="n-recentchanges" class="mw-list-item"><a href="/index.php?title=Special:RecentChanges" title="A list of recent changes in the wiki [r]" accesskey="r"><span>Recent changes</span></a></li><li id="n-randompage" class="mw-list-item"><a href="/index.php?title=Special:Random" title="Load a random page [x]" accesskey="x"><span>Random page</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> <a href="/index.php?title=Haskell" class="mw-logo"> <span class="mw-logo-container"> <strong class="mw-logo-wordmark">HaskellWiki</strong> </span> </a> </div> <div class="vector-header-end"> <div id="p-search" role="search" class="vector-search-box-vue vector-search-box-collapses vector-search-box-show-thumbnail vector-search-box-auto-expand-width vector-search-box"> <a href="/index.php?title=Special:Search" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only search-toggle" id="" title="Search HaskellWiki [f]" accesskey="f"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>Search</span> </a> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail cdx-typeahead-search--auto-expand-width"> <form action="/index.php" id="searchform" class="cdx-search-input cdx-search-input--has-end-button"> <div id="simpleSearch" class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="Search HaskellWiki" aria-label="Search HaskellWiki" autocapitalize="sentences" title="Search HaskellWiki [f]" accesskey="f" id="searchInput" > <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="Special:Search"> </div> <button class="cdx-button cdx-search-input__end-button">Search</button> </form> </div> </div> </div> <nav class="vector-user-links" aria-label="Personal tools" role="navigation" > <div id="p-vector-user-menu-overflow" class="vector-menu mw-portlet mw-portlet-vector-user-menu-overflow" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-login-2" class="user-links-collapsible-item mw-list-item"><a href="/index.php?title=Special:UserLogin&returnto=Maybe" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o"><span>Log in</span></a></li> </ul> </div> </div> <div id="vector-user-links-dropdown" class="vector-dropdown vector-user-menu vector-button-flush-right vector-user-menu-logged-out" title="More options" > <input type="checkbox" id="vector-user-links-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-user-links-dropdown" class="vector-dropdown-checkbox " aria-label="Personal tools" > <label id="vector-user-links-dropdown-label" for="vector-user-links-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-ellipsis mw-ui-icon-wikimedia-ellipsis"></span> <span class="vector-dropdown-label-text">Personal tools</span> </label> <div class="vector-dropdown-content"> <div id="p-personal" class="vector-menu mw-portlet mw-portlet-personal" title="User menu" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-login" class="user-links-collapsible-item mw-list-item"><a href="/index.php?title=Special:UserLogin&returnto=Maybe" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o"><span class="vector-icon mw-ui-icon-logIn mw-ui-icon-wikimedia-logIn"></span> <span>Log in</span></a></li> </ul> </div> </div> </div> </div> </nav> </div> </header> </div> <div class="mw-page-container"> <div class="mw-page-container-inner"> <div class="vector-sitenotice-container"> <div id="siteNotice"></div> </div> <div class="vector-main-menu-container"> <div id="mw-navigation"> <nav id="mw-panel" class="vector-main-menu-landmark" aria-label="Site" role="navigation"> <div id="vector-main-menu-pinned-container" class="vector-pinned-container"> </div> </nav> </div> </div> <input type="checkbox" id="vector-toc-collapsed-checkbox" class="vector-menu-checkbox"> <nav id="mw-panel-toc" role="navigation" aria-label="Contents" data-event-name="ui.sidebar-toc" class="mw-table-of-contents-container vector-toc-landmark vector-sticky-pinned-container"> <div id="vector-toc-pinned-container" class="vector-pinned-container"> <div id="vector-toc" class="vector-toc vector-pinnable-element"> <div class="vector-pinnable-header vector-toc-pinnable-header vector-pinnable-header-pinned" data-feature-name="toc-pinned" data-pinnable-element-id="vector-toc" > <h2 class="vector-pinnable-header-label">Contents</h2> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-toc.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-toc.unpin">hide</button> </div> <ul class="vector-toc-contents" id="mw-panel-toc-list"> <li id="toc-mw-content-text" class="vector-toc-list-item vector-toc-level-1"> <a href="#" class="vector-toc-link"> <div class="vector-toc-text">Beginning</div> </a> </li> <li id="toc-Type_equation" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Type_equation"> <div class="vector-toc-text"> <span class="vector-toc-numb">1</span>Type equation</div> </a> <ul id="toc-Type_equation-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Comparison_to_imperative_languages" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Comparison_to_imperative_languages"> <div class="vector-toc-text"> <span class="vector-toc-numb">2</span>Comparison to imperative languages</div> </a> <ul id="toc-Comparison_to_imperative_languages-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Classes" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Classes"> <div class="vector-toc-text"> <span class="vector-toc-numb">3</span>Classes</div> </a> <button aria-controls="toc-Classes-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon vector-icon--x-small mw-ui-icon-wikimedia-expand"></span> <span>Toggle Classes subsection</span> </button> <ul id="toc-Classes-sublist" class="vector-toc-list"> <li id="toc-Maybe_as_a_Monad" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Maybe_as_a_Monad"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.1</span>Maybe as a Monad</div> </a> <ul id="toc-Maybe_as_a_Monad-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Library_functions" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Library_functions"> <div class="vector-toc-text"> <span class="vector-toc-numb">4</span>Library functions</div> </a> <ul id="toc-Library_functions-sublist" class="vector-toc-list"> </ul> </li> </ul> </div> </div> </nav> <div class="mw-content-container"> <main id="content" class="mw-body" role="main"> <header class="mw-body-header vector-page-titlebar"> <label id="vector-toc-collapsed-button" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet vector-button-flush-left cdx-button--icon-only" for="vector-toc-collapsed-checkbox" role="button" aria-controls="vector-toc" tabindex="0" title="Table of Contents"> <span class="vector-icon mw-ui-icon-wikimedia-listBullet"></span> <span>Toggle the table of contents</span> </label> <nav role="navigation" aria-label="Contents" class="vector-toc-landmark"> <div id="vector-page-titlebar-toc" class="vector-dropdown vector-page-titlebar-toc vector-button-flush-left" > <input type="checkbox" id="vector-page-titlebar-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-titlebar-toc" class="vector-dropdown-checkbox " aria-label="Toggle the table of contents" > <label id="vector-page-titlebar-toc-label" for="vector-page-titlebar-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">Toggle the table of contents</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-titlebar-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">Maybe</span></h1> </header> <div class="vector-page-toolbar"> <div class="vector-page-toolbar-container"> <div id="left-navigation"> <nav aria-label="Namespaces"> <div id="p-associated-pages" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-associated-pages" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-nstab-main" class="selected vector-tab-noicon mw-list-item"><a href="/index.php?title=Maybe" title="View the content page [c]" accesskey="c"><span>Page</span></a></li><li id="ca-talk" class="new vector-tab-noicon mw-list-item"><a href="/index.php?title=Talk:Maybe&action=edit&redlink=1" rel="discussion" title="Discussion about the content page (page does not exist) [t]" accesskey="t"><span>Discussion</span></a></li> </ul> </div> </div> <div id="p-variants" class="vector-dropdown emptyPortlet" > <input type="checkbox" id="p-variants-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-p-variants" class="vector-dropdown-checkbox " aria-label="Change language variant" > <label id="p-variants-label" for="p-variants-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">English</span> </label> <div class="vector-dropdown-content"> <div id="p-variants" class="vector-menu mw-portlet mw-portlet-variants emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </nav> </div> <div id="right-navigation" class="vector-collapsible"> <nav aria-label="Views"> <div id="p-views" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-views" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-view" class="selected vector-tab-noicon mw-list-item"><a href="/index.php?title=Maybe"><span>Read</span></a></li><li id="ca-viewsource" class="vector-tab-noicon mw-list-item"><a href="/index.php?title=Maybe&action=edit" title="This page is protected. You can view its source [e]" accesskey="e"><span>View source</span></a></li><li id="ca-history" class="vector-tab-noicon mw-list-item"><a href="/index.php?title=Maybe&action=history" title="Past revisions of this page [h]" accesskey="h"><span>View history</span></a></li> </ul> </div> </div> </nav> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-dropdown" class="vector-dropdown vector-page-tools-dropdown" > <input type="checkbox" id="vector-page-tools-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-tools-dropdown" class="vector-dropdown-checkbox " aria-label="Tools" > <label id="vector-page-tools-dropdown-label" for="vector-page-tools-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">Tools</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-tools-unpinned-container" class="vector-unpinned-container"> <div id="vector-page-tools" class="vector-page-tools vector-pinnable-element"> <div class="vector-pinnable-header vector-page-tools-pinnable-header vector-pinnable-header-unpinned" data-feature-name="page-tools-pinned" data-pinnable-element-id="vector-page-tools" data-pinned-container-id="vector-page-tools-pinned-container" data-unpinned-container-id="vector-page-tools-unpinned-container" > <div class="vector-pinnable-header-label">Tools</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-page-tools.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-page-tools.unpin">hide</button> </div> <div id="p-cactions" class="vector-menu mw-portlet mw-portlet-cactions emptyPortlet vector-has-collapsible-items" title="More options" > <div class="vector-menu-heading"> Actions </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-more-view" class="selected vector-more-collapsible-item mw-list-item"><a href="/index.php?title=Maybe"><span>Read</span></a></li><li id="ca-more-viewsource" class="vector-more-collapsible-item mw-list-item"><a href="/index.php?title=Maybe&action=edit"><span>View source</span></a></li><li id="ca-more-history" class="vector-more-collapsible-item mw-list-item"><a href="/index.php?title=Maybe&action=history"><span>View history</span></a></li> </ul> </div> </div> <div id="p-tb" class="vector-menu mw-portlet mw-portlet-tb" > <div class="vector-menu-heading"> General </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-whatlinkshere" class="mw-list-item"><a href="/index.php?title=Special:WhatLinksHere/Maybe" title="A list of all wiki pages that link here [j]" accesskey="j"><span>What links here</span></a></li><li id="t-recentchangeslinked" class="mw-list-item"><a href="/index.php?title=Special:RecentChangesLinked/Maybe" rel="nofollow" title="Recent changes in pages linked from this page [k]" accesskey="k"><span>Related changes</span></a></li><li id="t-specialpages" class="mw-list-item"><a href="/index.php?title=Special:SpecialPages" title="A list of all special pages [q]" accesskey="q"><span>Special pages</span></a></li><li id="t-print" class="mw-list-item"><a href="javascript:print();" rel="alternate" title="Printable version of this page [p]" accesskey="p"><span>Printable version</span></a></li><li id="t-permalink" class="mw-list-item"><a href="/index.php?title=Maybe&oldid=66413" title="Permanent link to this revision of this page"><span>Permanent link</span></a></li><li id="t-info" class="mw-list-item"><a href="/index.php?title=Maybe&action=info" title="More information about this page"><span>Page information</span></a></li><li id="t-cite" class="mw-list-item"><a href="/index.php?title=Special:CiteThisPage&page=Maybe&id=66413&wpFormIdentifier=titleform" title="Information on how to cite this page"><span>Cite this page</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> </div> </div> </div> <div class="vector-column-end"> <nav class="vector-page-tools-landmark vector-sticky-pinned-container" aria-label="Page tools"> <div id="vector-page-tools-pinned-container" class="vector-pinned-container"> </div> </nav> </div> <div id="bodyContent" class="vector-body" aria-labelledby="firstHeading" data-mw-ve-target-container> <div class="vector-body-before-content"> <div class="mw-indicators"> </div> <div id="siteSub" class="noprint">From HaskellWiki</div> </div> <div id="contentSub"><div id="mw-content-subtitle"></div></div> <div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-parser-output"><table border="1" align="right" cellpadding="4" cellspacing="0" style="margin: 0 0 1em 1em; background: white; border: 1px #aaa solid; border-collapse: collapse;"> <caption style="height:3em"><big><big>Maybe class (<a href="/index.php?title=Base_package" title="Base package">base</a>)</big></big> </caption> <tbody><tr> <td><tt>import <a rel="nofollow" class="external text" href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#t%3AMaybe">Data.Maybe</a></tt> </td></tr></tbody></table> <p>The <b>Maybe</b> type is defined as follows: </p> <hask><div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre>data Maybe a = Nothing | Just a deriving (Eq, Ord)</pre></div> <p>It allows the programmer to specify something may not be there. </p> <meta property="mw:PageProp/toc" /> <h2><span class="mw-headline" id="Type_equation">Type equation</span></h2> <p><b>Maybe</b> satisfies the <a href="/index.php?title=Type" title="Type">type</a> equation <math class="mwe-math-element" xmlns="http://www.w3.org/1998/Math/MathML"><mstyle displaystyle="true" scriptlevel="0"><mi>F</mi><mi>X</mi><mo>=</mo><mn>1</mn><mo>+</mo><mi>X</mi></mstyle></math>, where the functor <math class="mwe-math-element" xmlns="http://www.w3.org/1998/Math/MathML"><mstyle displaystyle="true" scriptlevel="0"><mi>F</mi></mstyle></math> takes a set to a point plus that set. </p> <h2><span class="mw-headline" id="Comparison_to_imperative_languages">Comparison to imperative languages</span></h2> <p>Imperative languages may support this by rewriting as a <i>union</i> or allow one to use / return <i>NULL</i> (defined in some manner) to specify a value might not be there. </p> <h2><span class="mw-headline" id="Classes">Classes</span></h2> <p>As one can see from the <a href="/index.php?title=Type" title="Type">type</a> definition, <b>Maybe</b> will be an instance of <a href="/index.php?title=Eq&action=edit&redlink=1" class="new" title="Eq (page does not exist)">Eq</a> and <a href="/index.php?title=Ord&action=edit&redlink=1" class="new" title="Ord (page does not exist)">Ord</a> when the base type is. As well, instances of <a href="/index.php?title=Functor" title="Functor">Functor</a> and <a href="/index.php?title=Monad" title="Monad">Monad</a> are defined for <b>Maybe</b>. </p><p>For Functor, the <code>fmap</code> function moves inside the <b>Just</b> <a href="/index.php?title=Constructor" title="Constructor">constructor</a> and is identity on the <b>Nothing</b> <a href="/index.php?title=Constructor" title="Constructor">constructor</a>. </p><p>For Monad, the bind operation passes through <b>Just</b>, while <b>Nothing</b> will force the result to always be <b>Nothing</b>. </p> <h3><span class="mw-headline" id="Maybe_as_a_Monad">Maybe as a Monad</span></h3> <p>Using the <a href="/index.php?title=Monad" title="Monad">Monad</a> class definition can lead to much more compact code. For example: </p> <hask><div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre> f :: Int -> Maybe Int f 0 = Nothing f x = Just x g :: Int -> Maybe Int g 100 = Nothing g x = Just x h :: Int -> Maybe Int h x = case f x of Just n -> g n Nothing -> Nothing h' :: Int -> Maybe Int h' x = do n <- f x g n</pre></div> <p>The functions <code>h</code> and <code>h'</code> will give the same results. (<math class="mwe-math-element" xmlns="http://www.w3.org/1998/Math/MathML"><mstyle displaystyle="true" scriptlevel="0"><mi>h</mi><mn>0</mn><mo>=</mo><msup><mi>h</mi><mo>′</mo></msup><mn>0</mn><mo>=</mo><mi>h</mi><mn>1</mn><mn>0</mn><mn>0</mn><mo>=</mo><msup><mi>h</mi><mo>′</mo></msup><mn>1</mn><mn>0</mn><mn>0</mn><mo>=</mo><mi>N</mi><mi>o</mi><mi>t</mi><mi>h</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo>;</mo><mspace width="0.5em"></mspace><mi>h</mi><mi>x</mi><mo>=</mo><msup><mi>h</mi><mo>′</mo></msup><mi>x</mi><mo>=</mo><mi>J</mi><mi>u</mi><mi>s</mi><mi>t</mi><mstyle scriptlevel="0"><mspace width="0.167em"></mspace></mstyle><mi>x</mi></mstyle></math>). In this case the savings in code size is quite modest, stringing together multiple functions like <code>f</code> and <code>g</code> will be more noticeable. </p> <h2><span class="mw-headline" id="Library_functions">Library functions</span></h2> <p>When the module is imported, it supplies a variety of useful functions including: </p> <dl><dt><hask><code class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr">maybe :: b->(a->b) -> Maybe a -> b</code></hask></dt> <dd>Applies the second argument to the third, when it is <i>Just x</i>, otherwise returns the first argument.</dd> <dt><hask><code class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr">isJust</code>, <hask><code class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr">isNothing</code></hask></hask></dt> <dd>Test the argument, returing a Bool based on the constructor.</dd> <dt><hask><code class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr">listToMaybe</code>, <hask><code class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr">maybeToList</code></hask></hask></dt> <dd>Convert to/from a one element or empty list.</dd> <dt><hask><code class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr">mapMaybe</code></hask></dt> <dd>A different way to filter a list.</dd></dl> <p>See the documentation for <a rel="nofollow" class="external text" href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html">Data.Maybe</a> for more explanation and other functions. </p></hask></hask> <!-- NewPP limit report Cached time: 20250403114351 Cache expiry: 86400 Reduced expiry: false Complications: [show鈥恡oc] CPU time usage: 0.020 seconds Real time usage: 0.038 seconds Preprocessor visited node count: 116/1000000 Post鈥恊xpand include size: 393/2097152 bytes Template argument size: 42/2097152 bytes Highest expansion depth: 3/100 Expensive parser function count: 8/100 Unstrip recursion depth: 1/20 Unstrip post鈥恊xpand size: 2610/5000000 bytes --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 2.001 1 Template:Standard_type 100.00% 2.001 1 -total --> <!-- Saved in parser cache with key mediawiki:pcache:idhash:1598-0!canonical and timestamp 20250403114351 and revision id 66413. Rendering was triggered because: page-view --> </div> <div class="printfooter" data-nosnippet="">Retrieved from "<a dir="ltr" href="https://wiki.haskell.org/index.php?title=Maybe&oldid=66413">https://wiki.haskell.org/index.php?title=Maybe&oldid=66413</a>"</div></div> <div id="catlinks" class="catlinks" data-mw="interface"><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="/index.php?title=Special:Categories" title="Special:Categories">Categories</a>: <ul><li><a href="/index.php?title=Category:Pages_with_syntax_highlighting_errors&action=edit&redlink=1" class="new" title="Category:Pages with syntax highlighting errors (page does not exist)">Pages with syntax highlighting errors</a></li><li><a href="/index.php?title=Category:Standard_types" title="Category:Standard types">Standard types</a></li><li><a href="/index.php?title=Category:Monad" title="Category:Monad">Monad</a></li></ul></div></div> </div> </main> </div> <div class="mw-footer-container"> <footer id="footer" class="mw-footer" role="contentinfo" > <ul id="footer-info"> <li id="footer-info-lastmod"> This page was last edited on 11 November 2023, at 00:28.</li> <li id="footer-info-copyright">Recent content is available under <a href="/index.php?title=HaskellWiki:Copyrights" title="HaskellWiki:Copyrights">simple permissive license</a>.</li> </ul> <ul id="footer-places"> <li id="footer-places-privacy"><a href="/index.php?title=HaskellWiki:Privacy_policy">Privacy policy</a></li> <li id="footer-places-about"><a href="/index.php?title=HaskellWiki:About">About HaskellWiki</a></li> <li id="footer-places-disclaimers"><a href="/index.php?title=HaskellWiki:General_disclaimer">Disclaimers</a></li> </ul> <ul id="footer-icons" class="noprint"> <li id="footer-copyrightico"><a href="https://wiki.haskell.org/HaskellWiki:Copyrights"><img src="" alt="simple permissive license" width="88" height="31" loading="lazy"></a></li> </ul> </footer> </div> </div> </div> <div class="vector-settings" id="p-dock-bottom"> <ul> <li> <button class="cdx-button cdx-button--icon-only vector-limited-width-toggle" id=""><span class="vector-icon mw-ui-icon-fullScreen mw-ui-icon-wikimedia-fullScreen"></span> <span>Toggle limited content width</span> </button> </li> </ul> </div> <script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgBackendResponseTime":32,"wgPageParseReport":{"limitreport":{"cputime":"0.020","walltime":"0.038","ppvisitednodes":{"value":116,"limit":1000000},"postexpandincludesize":{"value":393,"limit":2097152},"templateargumentsize":{"value":42,"limit":2097152},"expansiondepth":{"value":3,"limit":100},"expensivefunctioncount":{"value":8,"limit":100},"unstrip-depth":{"value":1,"limit":20},"unstrip-size":{"value":2610,"limit":5000000},"timingprofile":["100.00% 2.001 1 Template:Standard_type","100.00% 2.001 1 -total"]},"cachereport":{"timestamp":"20250403114351","ttl":86400,"transientcontent":false}}});});</script> </body> </html>