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. C++11 function

    #include <iostream> #include <functional> #include <vector> using namespace std; / ...

  2. cmd 递归删除目录或文件

    递归删目录 for /r <TARGET DIR> %i in (<DIR NAME or Pattern>) do rd /s /q %i 递归删文件 for /r < ...

  3. 在ASP.NET MVC中使用Knockout实践04,控制View Model的json格式内容

    通常,需要把View Model转换成json格式传给服务端.但在很多情况下,View Model既会包含字段,还会包含方法,我们只希望把字段相关的键值对传给服务端. 先把上一篇的Product转换成 ...

  4. MyBatis-Generator最佳实践

    引用地址:http://arccode.net/2015/02/07/MyBatis-Generator%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/ 最近使用MyBati ...

  5. javascript游戏引擎

    基于JavaScript开发的游戏是唯一一个能够跨桌面,Web和移动三种平台的.今天,本文向大家推荐一些非常棒的JavaScript游戏开发框架. AD:干货来了,不要等!WOT2015 北京站演讲P ...

  6. SharePoint Online 设置网站集

    前言 本文介绍如何在Office 365中设置SharePoint Online网站集,当我们创建好SharePoint Online站点,开始使用之前,一定会有一些基本的设置,本文就为大家介绍这些基 ...

  7. CCLabelAtlas的宽度为奇数时的显示bug

    遇到一个很郁闷的bug,CCLabelAtlas设置文字内容在ipad上和android上正常,就只有iphone怎么显示都不正常.后来把它宽度 + 1,然后就正常了.发现以前宽度设置为21px.23 ...

  8. Adapter数据变化改变现有View的实现原理及案例

    首先说说Adapter详细的类的继承关系.例如以下图 Adapte为接口它的实现类的对象作为AdapterView和View的桥梁,Adapter是装载了View(比方ListView和girdVie ...

  9. [Web 前端] webstorm 快速搭建react项目

    cp from : https://blog.csdn.net/qq_39207948/article/details/79467144 前端新手如何安装webstorm ,初步搭建react项目 下 ...

  10. [Web 前端] CSS篇之3. 如何保持浮层水平垂直居中

    原文链接](http://www.cnblogs.com/yaliu/p/5190957.html) 浮层水平垂直居中方法 (一)利用绝对定位与transform <div class=&quo ...