[PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching
Pattern matching in functional programming languages is a way to break up expressions into individual cases.
We are going to go through how to pattern match in PureScript with simple patterns, guards, array patterns and record patterns.
greater :: Int -> Int -> Int
greater n m | n > m = n -- '|' is called guard, the same as if else
| otherwise = m isEmpty :: forall a. Array a -> Boolean -- a in array should have same type
isEmpty [] = true
isEmpty _ = false
main = render =<< withConsole do
log $ show $ greater 3 2 -- 3
log $ show $ greater 11 22 -- 22
log $ show $ isEmpty [] -- true
log $ show $ isEmpty [1, 2] -- false
--
Full code:
module Main where import Prelude
import Control.Monad.Eff.Console (log)
import TryPureScript myTypes :: Int
myTypes = 1 -- add (a -> (b -> (a + b)))
add :: Int -> Int -> Int
add a b = a + b
addMe = \a -> \b -> a + b -- inc (a -> (add 1 a))
inc :: Int -> Int
inc = add 1 -- Data constructors
data FooType = Foo | Bar String runFoo :: FooType -> String
-- runFoo take a param Foo which should be string
runFoo Foo = "it is foo"
-- runFoo also can take Bar and String
-- <> is similar to str.concat isn JS
runFoo (Bar s) = "Yeah it's Bar and " <> s greater :: Int -> Int -> Int
greater n m | n > m = n -- '|' is called guard, the same as if else
| otherwise = m isEmpty :: forall a. Array a -> Boolean -- a in array should have same type
isEmpty [] = true
isEmpty _ = false main = render =<< withConsole do
log $ show $ greater 3 2
log $ show $ greater 11 22
log $ show $ isEmpty []
log $ show $ isEmpty [1, 2]
[PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching的更多相关文章
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- Stanford parser:入门使用
一.stanford parser是什么? stanford parser是stanford nlp小组提供的一系列工具之一,能够用来完成语法分析任务.支持英文.中文.德文.法文.阿拉伯文等多种语言. ...
- MySQL所有函数及操作符
参考:Function and Operator Reference Name Description ABS() Return the absolute value ACOS() Return th ...
- <Scala><For beginners>
Scala Overview Scala is object-oriented Scala is a pure object-oriented language in the sense that e ...
- DRDS SQL兼容性
SQL 兼容性 更新时间:2017-06-07 13:26:11 DRDS 高度兼容 MySQL 协议和语法,但由于分布式数据库和单机数据库存在较大的架构差异,存在 SQL 使用限制.相关兼容 ...
- Regular Expressions --正则表达式官方教程
http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...
- 8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
- [Regular Expressions] Find Plain Text Patterns
The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look a ...
- [转]8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
随机推荐
- STM32定时器级联 -- AN2592
Master configuration When a timer is selected as a master timer, the corresponding trigger output si ...
- 零宽断言 -- Lookahead/Lookahead Positive/Negative
http://www.vaikan.com/regular-expression-to-match-string-not-containing-a-word/ 经常我们会遇到想找出不包含某个字符串的文 ...
- .NET轻量级DBHelpers数据访问组件
一.摘要 一说到ADO.NET大家可能立刻想到的就是增.删.改.查(CRUD)操作,然后再接就想到项目中的SQLHelper.没错本课分享课阿笨给大家带来的是来源于github上开源的DAO数据库访问 ...
- delphi teechrt中TChart 一些属性设置
把图片设置成黑白 2.设置颜色
- Swift iOS 文件操作:沙盒(SandBox)、程序包(NSBundle)
1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程序都有一个单独的文件系统(存储空间),而且只能在对应的文件系统中进行操作,此区域被称为沙盒.所有的非代码文件 ...
- 在ASP.NET MVC实现购物车,尝试一种不同于平常的购物车显示方式
通常,我们看到的购物车是这样的: 虽然这种购物车显示方式被广泛运用,但我个人觉得不够直观.如果换成这样呢? 本篇的源码放在了:https://github.com/darrenji/ShoppingC ...
- 报错:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
在保存数据的时候报这个错误,知道是验证错误,但到底是哪个属性验证错误呢? →打断点,运行,观察程序出错的地方→在出错的部分添加try...catch语句块→添加监视,输入((System.Data.E ...
- 在ASP.NET MVC中使用Knockout实践08,使用foreach绑定集合
本篇体验使用 foreach 绑定一个Product集合. 首先使用构造创建一个View Model. var Product = function(data) { this.name = ko.ob ...
- javascript:addEventListener
addEventListener 用于注册事件处理程序,IE 中为 attachEvent,我们为什么讲 addEventListener 而不讲 attachEvent 呢?一来 attachEve ...
- Netty 包头
LengthFieldBasedFrameDecoder 常用的处理大数据分包传输问题的解决类,先对构造方法LengthFieldBasedFrameDecoder中的参数做以下解释说明 maxFra ...