`

haskell- syntax in functions

阅读更多

In this post we will examine some of the cool syntax that  involves hte discussion of pattern matching, we will then discuss some advanced topic such as guard, where clause, let in and case statement, in summary the 

 

  • guard
  • where clause
  • let in 
  • case statement

wll have it special syntax. 

The topic list of this post will comprise of the following.


  • function signature 
    • type constraint
    • parameter types
  • pattern matching
    • on single value such as String, Int
    • pattern matching on list    
    • match variables
    • wildcards (_)
    • entity called 'patterns' to refer to matched variable

 

The code is as follow. 

-- author
--   boqwang
-- refernces
--  http://learnyouahaskell.com/syntax-in-functions


-- pattern matching
--  syntac constructs and starts with the pattern matching.

lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"  
lucky x = "Sorry, you're out of luck, pal!"


sayMe :: (Integral a) => a -> String
sayMe 1 = "One!"  
sayMe 2 = "Two!"  
sayMe 3 = "Three!"  
sayMe 4 = "Four!"  
sayMe 5 = "Five!"  
sayMe x = "Not between 1 and 5" 



-- and we will define factorial again
-- however, this time not with number n as produce [1 .. n]
--  NOTE:
--    you would not write the second factorial pattern first
--    because doing that will probablly catch all cases, including 0 
factorial :: (Integral a ) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)


-- non-exhaustive patterns

charName :: Char -> String
charName 'a' = "Albert"
charName 'b' = "Broseph"  
charName 'c' = "Cecil" 

-- and if you call
--  charName 'h'
-- you will get an 
--   "*** Exception: tut.hs:(53,0)-(55,21): Non-exhaustive patterns in function charName 

-- patterns on tuples

-- first a non-pattern matching definition
-- addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)					   
-- addVectors a b = (fst a + fst b, snd a + snd b) 


-- then a pattern one    

addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)
addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2) -- we guarantee to have two pairs as parameters 





-- define fst, snd on triples

first :: (a, b, c) -> a
first (x, _, _) = x

second :: (a, b, c) -> b
second (_, y, _) = y

-- and the "third"
third :: (a, b, c) -> c
third (_, _, z) = z


-- postscript/addendum/epilogue/appendix/postlude

-- _ means we don't care
--   we have seen this "length' xs = sum [1 | _ <- xs ]"
-- it means wildcards in general


-- review the list comprehension
--   we are actually using pattern matching in List comprehension
-- let xs = [(1,3), (4,3), (2,4), (5,3), (5,6), (3,1)] 
-- [a + b | (a, b) <- xs]


-- Lists themselves can be used in pattern matching
--  [1,2,3] is just syntactic sugar to 1:2:3:[]
--  and x:xs will bind head of hte list to x and the rest of it to xs


-- the head function
head' :: [a] -> a
head' [] = error "can't call head on an empty list, dummy!"
head' (x:_) = x

-- test if that works
-- head' [4,5,6]
--  head' "hello"


-- pattern matching a trivial case

tell :: (Show a) => [a] -> String  
tell [] = "The list is empty"  
tell (x:[]) = "The list has one element: " ++ show x  
tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y  
tell (x:y:_) = "This list is long. The first two elements are: " ++ show x ++ " and " ++ show y


-- as we said, the length function provided by the built-in function is lame
-- we will provide our own

-- if you mistake (Num b) => [b] -> a, there would be a " Could not deduce (Num a) from the context (Num b)" error!
length' :: (Num b) => [a] -> b
length' [] = 0 
-- since we don't care much about the variable binding below
-- legnth' (x:xs) = 1 + length' xs
length' (_ :xs) = 1 + length' xs


-- and we can re-implements sum function as well 

sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs


-- the things called as "pattern"
capital :: String -> String
capital "" = "Empty string, whoops!"  
capital all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x]

-- capital "Dracula"


-- what you can not do ?
-- (xs ++ [x, y, z] or (xs ++ [x])

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-in-haskell:试图在Haskell中编写可理解的Haskell实现

    安装过程中应在路径上放置一个名为haskell-in-haskell的可执行文件。 否则,您可以使用cabal run haskell-in-haskell --直接运行项目,然后输入要传递的参数。 编译中 要编译Haskell文件,只需运行: haskell-in-...

    Programming-in-Haskell-2nd-Edition.pdf

    《Programming in Haskell》第二版是Graham Hutton撰写的一本关于Haskell编程语言的经典教材。这本书深入浅出地介绍了函数式编程的概念,并专注于Haskell这一纯函数式编程语言的细节和应用。对于想要理解函数式编程...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    PPT of Programming-in-Haskell-2nd-Edition

    Progrtamming in Haskell 2nd edition ppt仅供学习,禁止用于商业行为。Progrtamming in Haskell 2nd edition ppt仅供学习,禁止用于商业行为。Progrtamming in Haskell 2nd edition ppt仅供学习,禁止用于商业行为...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    haskell-brainfuck:Haskel 脑残翻译

    haskell-brainfuck 解释 haskel-brainfuck 作为库分发,但它也包含一个可执行文件来运行 Brainfuck 程序。 你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as ...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

Global site tag (gtag.js) - Google Analytics