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. THE TOOLS TO MANAGE YOUR DATA ACROSS CLOUDS

    http://blog.grexit.com/manage-data-across-clouds/ That the average small business uses a cloud servi ...

  2. Apache Mina Filter

    Mina中的过滤器处于IoService与IoHandler之间,用于过滤每一个I/O事件.本文分析Mina中的过滤器是怎么串起来的? 前面提到了IoFilter,FilterChain等接口和类,在 ...

  3. jQuery Ajax 上传文件夹及文件

    我们先来看一下文件夹结构 这是上传处理的: 看一下系统日志: 升级 HTML5文件实现拖拽上传提示效果改进(支持三种状态提示) 拖拽过程详解: 1:文件未拖出文件选择框的时候提示:将要上传的文件或文件 ...

  4. 苹果无法连接到itunes store怎么办

    方法1:设置--还原--还原网络设置,再进app store就可以了.方法2:重置访问限制“设置”–> “通用” –> “访问限制”,开启访问限制5秒,然后再关闭访问限制.方法3:重置当前 ...

  5. 吸血鬼猎人巴菲第一至八季/全集Buffy迅雷下载

    本季看点:<吸血鬼猎人巴菲>故事背景在现代,话说于每一个世代都会出现一个年青的女孩子,在人世间寻找及对付一些妖魔鬼怪,例如有吸血鬼.坏女巫等等邪恶的势力,而这个年青的女孩子则被称为Slay ...

  6. sublime text的扩展插件

    sublime text用作开发编辑器,还缺省二个比较重要功能:跨文件跳转.返回最后一次编辑的位置: 这里有二个插件正好解决此问题:CTags.ChangeList   其它常用的插件,google一 ...

  7. 7z文件格式及其源码linux/windows编译

    7z文件格式及其源码的分析(二) 一. 准备工作: 1. 源码下载: 可以从官方中文主页下载:http://sparanoid.com/lab/7z/. 为了方便, 这里直接给出下载链接: http: ...

  8. ARCH模型

    ARCH模型的基本思想 ARCH模型的基本思想是指在以前信息集下,某一时刻一个噪声的发生是服从正态分布.该正态分布的均值为零,方差是一个随时间变化的量(即为条件异方差).并且这个随时间变化的方差是过去 ...

  9. maven项目里,junit的test程序不能访问src/test/resource下面的配置

    问题描述 最近在写单元测试,但是不想改动源代码,所以想自己在本test目录下建一个resouces文件夹并添加对应的配置文件,可是发现test程序无法读取这个resouces文件夹下的配置. 问题解决 ...

  10. C#高级编程六十六天----表达式树总结【转】

    https://blog.csdn.net/shanyongxu/article/details/47257139 表达式树总结 基础 表达式树提供了一个将可执行代码转换成数据的方法.如果你要在执行代 ...