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的更多相关文章

  1. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  2. Stanford parser:入门使用

    一.stanford parser是什么? stanford parser是stanford nlp小组提供的一系列工具之一,能够用来完成语法分析任务.支持英文.中文.德文.法文.阿拉伯文等多种语言. ...

  3. MySQL所有函数及操作符

    参考:Function and Operator Reference Name Description ABS() Return the absolute value ACOS() Return th ...

  4. <Scala><For beginners>

    Scala Overview Scala is object-oriented Scala is a pure object-oriented language in the sense that e ...

  5. DRDS SQL兼容性

    SQL 兼容性 更新时间:2017-06-07 13:26:11     DRDS 高度兼容 MySQL 协议和语法,但由于分布式数据库和单机数据库存在较大的架构差异,存在 SQL 使用限制.相关兼容 ...

  6. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  7. 8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  8. [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 ...

  9. [转]8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

随机推荐

  1. 领域Model?

    前言 领域驱动设计里有很多东西,我们可以应用在各种各样的开发模式里,所以接下来说的一些东西,我们可以部分使用. 说道领域驱动的领域,大家肯定就要开始说Bounded Context,聚合,聚合根,容易 ...

  2. MySQL是如何利用索引的

    http://fordba.com/spend-10-min-to-understand-how-mysql-use-index.html

  3. 为Qemu aarch32添加BeautifulSoup4模块

    环境 Qemu:2.8.0 开发板:vexpress-ca9   概述 上一篇博文已经可以让我们的开发板可以成功的ping通百度了,据说Python的网络功能也很强大,而Beautiful Soup是 ...

  4. java ffmpeg视频转码(自测通过)

    import java.io.*; public class VideoTransfer { //ffmepg文件 安装目录 private static String ffmpeg = " ...

  5. Informix存储过程

    一.存储过程概述 存储过程是一个用户定义的函数,由存储过程语句(SPL) 和一组SQL语句组成,以可以执行代码形式存储在数据库中,和表.视图.索引等一样,是数据库的一种对象. 存储过程语言SPL(St ...

  6. 分布式系统唯一ID生成方案汇总 转

    系统唯一ID是我们在设计一个系统的时候常常会遇见的问题,也常常为这个问题而纠结.生成ID的方法有很多,适应不同的场景.需求以及性能要求.所以有些比较复杂的系统会有多个ID生成的策略.下面就介绍一些常见 ...

  7. 每天一个linux命令-wc命令

    语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...

  8. Eclipse 乱码 解决方案总结(UTF8 -- GBK)

    UTF8 --> GBK;   GBK --> UTF8 eclipse的中文乱码问题,一般不外乎是由操作系统平台编码的不一致导致,如Linux中默认的中文字体编码问UTF8, 而Wind ...

  9. 【转】windows下mongodb安装与使用整理

    转自 :http://www.cnblogs.com/lecaf/archive/2013/08/23/mongodb.html 一.首先安装mongodb 1.下载地址:http://www.mon ...

  10. Intelli公司IDE快捷键设置

    自定义快捷键:Keymap 搜索某个像自定义的快捷键,看被占用了没有:搜索时,直接键盘敲击快捷键,比如command+T