PureScript types are very extensive and we are going to experiment with type constructors and how to make then use them.

We can define a custom data type:

-- Data constructors
data FooType = Foo | Bar String

Define a function, take FooType, return a 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

Because FooType is Unit type, we need to define both 'Foo' and '(Bar s)'.

main = render =<< withConsole do
log $ show $ runFoo (Bar "Wan") -- "Yeah it's Bar and Wan"
log $ show $ runFoo Foo -- "it is foo"

--

FullCode:

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 main = render =<< withConsole do
log $ show $ runFoo (Bar "Wan") -- "Yeah it's Bar and Wan"
log $ show $ runFoo Foo -- "it is foo"

[PureScript] Basic Data Constructors in PureScript的更多相关文章

  1. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. Basic Data Structure

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. Basic Data Structure HDU - 5929 (这个模拟我要报警了)

    Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...

  6. [OpenCV] Basic data types - Matrix

    http://docs.opencv.org/2.4.13/ Basis 矩形 "modules/core/src/drawing.cpp" CV_IMPL void cvRect ...

  7. 1.6 Indexing and Basic Data Operations--目录

    1.6.1 什么是 Indexing 1.6.2 Uploading Data with Index Handlers 1.6.3 Uploading Data with Solr Cell usin ...

  8. Basic Data Structures and Algorithms in the Linux Kernel--reference

    http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...

  9. Chapter 04—Basic Data Management

    1. 创建新的变量 variable<-expression expression:包含一组大量的操作符和函数.常用的算术操作符如下表: 例1:根据已知变量,创建新变量的三种途径 > my ...

随机推荐

  1. 基于TQ2440和Qemu的GDB+串口调试(1)

    作者 彭东林 pengdonglin137@163.com   平台 TQ2440 + Linux-4.10.17 Qemu(vexpress-ca9) + Linux-4.10.17   概述 下面 ...

  2. 淡淡理解下AngularJS中的module

    在AngularJS中module是一个核心的存在,包括了很多方面,比如controller, config, service, factory, directive, constant, 等等. 在 ...

  3. 在ASP.NET MVC中实现本地化和全球化

    在开发多语言网站时,我们可以为某种语言创建一个资源文件,根据浏览器所设置的不同语言偏好,让运行时选择具体使用哪个资源文件.资源文件在生成程序集的时候被嵌入到程序集. 本篇体验,在ASP.NET MVC ...

  4. Linux source命令(转)

    Linux source命令: 通常用法:source filepath 或 . filepath 功能:使当前shell读入路径为filepath的shell文件并依次执行文件中的所有语句,通常用于 ...

  5. ios开发怎样才能做到代码和界面彻底分离,方便换肤?

    设想一下,你现在手底下有N个开发人员,你如何让这些人参与到一个ios开发项目中来?而不是独自一个人完成.

  6. HelloWorld 之JasperReports初步

    在企业应用系统中,经常要输出各种格式的数据报表. 著名的开源项目<JasperReports可以很好的解决这个问题. 使用JasperReports可以在预先设定好格式的报表基础上进行数据的填充 ...

  7. Java 数组的 12 个最佳方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  8. SurfaceFlinger( 226): Permission Denial: can't access SurfaceFlinger

    MODIFY_PHONE_STATE permission is granted to system apps only. For your information, there are 2 type ...

  9. Android 自动编译、打包生成apk文件 2 - 使用原生Ant方式

    from://http://blog.csdn.net/androiddevelop/article/details/11100109 相关文章列表: <Android 自动编译.打包生成apk ...

  10. 图标框架Font Awesome

    直接引入Font Awesome的css <!DOCTYPE html> <html lang="en"> <head> <meta ch ...