[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 ...
随机推荐
- 安装node.js / npm / express / KMC
http://www.cnblogs.com/seanlv/archive/2011/11/22/2258716.html 1. 下载Node.js官方Windows版程序: http://nodej ...
- linux虚拟机与winodows共享文件夹----linux安装VMware tools
虚拟机里面想要获取原来本机 系统的文件,十分麻烦.为了实现原系统与虚拟机的共享文件夹,可以通过安装vmware tools达到共享目的. 1 安装vmware tools (1)检查虚拟机上是否挂 ...
- Lucene 3.0 输出相似度
http://www.cnblogs.com/ibook360/archive/2011/10/19/2217638.html Lucene3.0之结果排序(原理篇) 传统上,人们将信息检索系统返回结 ...
- 测试RemObjects Pascal Script
unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...
- WordPress主题开发:开启feed功能
开启feed功能 步骤一:在模版文件的<head></head>元素中添加wp_head()函数,且wp_head()函数要放在</head>标签之前,而且紧邻&l ...
- EntityFramework:状态变化与方法的关系(朋友的学习笔记)
一.约定OnModelCreating 有一些限制需要注意,例如: 1.表名不支持使用标签进行标注 2.最小长度在 OnModelCreating 中不支持 3.正则表达式在 OnModelCreat ...
- ios之归档demo
ios对自定义对象的归档.首先需要实现NSCoding与NSCopying接口 #import <Foundation/Foundation.h> @interface Person : ...
- mysql 内存表
show variables like 'max_%'; max_heap_table_size 16777216 max_tmp_tables 32 show variables l ...
- GridView和SimpleAdapter实现网格布局
android:horizontalSpacing 元素之间的水平间距 android:verticalSpacing 元素之间的垂直间距 android:numColumns ...
- Forward secrecy
In cryptography, forward secrecy (FS), also known as perfect forward secrecy (PFS), is a property of ...