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. HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  2. jQuery自己定义绑定的魔法升级版

    jQuery自己定义绑定 首先让我们来看看jQuery的自己定义绑定的用法,你能够使用bind或者live来订阅一个事件(当然1.7以后也能够使用on了),代码例如以下: $("#myEle ...

  3. C++ STL set::find的用法

      参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - opera ...

  4. win开启远程链接(可以被连接)

    电脑-属性,这样打开了控制面板,坐车选择远程设置,选择允许任何人链接,然后关闭防火墙即可.

  5. Dropdown.js基于jQuery开发的轻量级下拉框插件

    Dropdown.js 前言 在SPA(Single Page Application)盛行的时代,jQuery插件的轮子正在减少,由于我厂有需求而开发了这个插件.如果觉得本文对您有帮助,请给个赞,以 ...

  6. 华为机试正式版(西安c/c++/java),今天下午去机试的题目,新奇出炉了!

    下面题目都是回顾的.题目都非常easy, 大家有些基础就能够參加!(语言能够是c/c++.也能够是java的) 题目一(60分): 字符串操作. 将小写转换成大写, 将大写转化为小写, 数字的不做转换 ...

  7. CAD二次开发起步

    1 环境搭建(VS 2017 + CAD 2019) CAD版本一般要比VS版本晚两年以上,比如我现在使用VS2017,则CAD要用2019版,至于CAD2018能不能用没试验过,有兴趣的小伙伴可以试 ...

  8. Cesium中的地形和坐标转换说明

    1 Cesium中的地形 Cesium中的地形系统是一种由流式瓦片数据生成地形mesh的技术,厉害指出在于其可以自动模拟出地面.海洋的三维效果.创建地形图层的方式如下: var terrainProv ...

  9. 【Centos】centos查看磁盘使用情况

    1.查看分区和磁盘 lsblk 查看分区和磁盘 2.查看空间使用情况 df -h 查看空间使用情况 3.分区工具查看分区信息 fdisk -l 分区工具查看分区信息 4.查看分区 cfdisk /de ...

  10. maven打包时跳过单元测试

    运行mvn install时跳过Test <project> [...] <build> <plugins> <plugin> <groupId& ...