This is a beginner's tutorial to let them understand the fundamentals of Haskell.
这是一个初学者的教程,让他们了解Haskell的基础知识。
什么是Haskell? (What Is Haskell?)
Haskell is a widely used purely functional language.
Haskell是一种广泛使用的纯功能语言。
Functional programs are more concurrent, and they follow parallelism in execution to provide more accurate and better performance. In typical programming languages, we instruct a compiler to do something and how to do it. — https://www.tutorialspoint.com/haskell/haskell_overview.htm
功能程序的并发性更高,它们在执行时遵循并行性,以提供更准确和更好的性能。 在典型的编程语言中,我们指示编译器执行某些操作以及如何执行该操作。 — https://www.tutorialspoint.com/haskell/haskell_overview.htm
That’s not so in Haskell, where we ask what it is we do. Also, Haskell is a sluggish programming language, so the program doesn’t execute code that it thinks is not necessary.
在Haskell并非如此,我们在那询问我们在做什么。 同样,Haskell是一种缓慢的编程语言,因此该程序不会执行它认为不必要的代码。
A Haskell program is nothing more than a series of functions that execute.
Haskell程序无非就是一系列执行的功能。
Haskell is a strictly typed language. By the term strictly typed language, we mean the Haskell compiler is intelligent enough to figure out the type of the variable declared. Hence we need not explicitly mention the style of the variable used.– https://www.tutorialspoint.com/haskell/haskell_overview.htm
Haskell是一种严格类型化的语言。 术语“ 严格类型化语言”是指Haskell编译器足够智能,可以弄清楚声明的变量的类型。 因此,我们无需明确提及所使用变量的样式。– https://www.tutorialspoint.com/haskell/haskell_overview.htm
入门 (Getting Started)
To set up a Haskell environment on your Windows computer, go to their official website https://www.haskell.org/platform/windows.html and download the installer.
要在Windows计算机上设置Haskell环境,请访问其官方网站https://www.haskell.org/platform/windows.html并下载安装程序。
To set up a Haskell environment on your MAC system, go to their official website https://www.haskell.org/platform/mac.html and download the Mac installer.
要在您的MAC系统上设置Haskell环境,请访问其官方网站https://www.haskell.org/platform/mac.html并下载Mac安装程序。
For Linux, the installation process is as follows:
对于Linux,安装过程如下:
$ sudo apt-get install haskell-platform
启动Haskell (Starting Haskell)
It’s effortless to start the Haskell programming language, and you enter the following command in the terminal:
启动Haskell编程语言非常容易,您可以在终端中输入以下命令:
$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>
Now, you can enter your Haskell code, for example:
现在,您可以输入您的Haskell代码,例如:
范例程序 (Example program)
It is a simple example to demonstrate the dynamism of Haskell. Take a look at the following code:
这是一个简单的例子来说明Haskell的动态性。 看下面的代码:
Prelude> main = putStrLn "Hello Medium"
Output:
输出:
Hello Medium
基本操作 (Basic Operations)
Haskell is intelligent enough to decode some number as a number. Therefore, you need not mention its type externally as we usually do in other programming languages.
Haskell足够聪明,可以将某些数字解码为数字。 因此,您不必像我们通常在其他编程语言中那样在外部提到它的类型。
An example:
一个例子:
Prelude> 4*4
You will receive the output:
您将收到输出:
Prelude> 16
性格 (Characters)
Like numbers, Haskell can intelligently identify a character given as an input to it.
像数字一样,Haskell可以智能地识别作为输入输入的字符。
To display the type of a variable, you can enter the following line of code:
要显示变量的类型,可以输入以下代码行:
Prelude> :t "a"
This will display:
这将显示:
"a" :: [Char]
弦乐 (Strings)
A string is nothing but a collection of characters. We can create the following string:
字符串不过是字符的集合。 我们可以创建以下字符串:
Prelude> "Hello Medium"
It will display:
它将显示:
"Hello Medium"
If want to know the type of this string, we can use :t
again:
如果想知道这个字符串的类型,我们可以再次使用:t
:
Prelude> :t "Hello Medium"
The output will be:
输出将是:
"Hello Medium" :: [Char]
As I already said, a string is just a collection of characters, so this will be returned as a Char
data type.
正如我已经说过的,字符串只是字符的集合,因此它将作为Char
数据类型返回。
布尔值 (Booleans)
The boolean data type is pretty much as straightforward as other data types. In the example below, you see some booleans.
布尔数据类型与其他数据类型一样简单。 在下面的示例中,您将看到一些布尔值。
Prelude> True && True
Or
要么
Prelude> True && False
清单 (Lists)
Like other data types, List
is also a beneficial data type used in Haskell. As per example, [a,b,c] is a list of characters. Hence, by definition, List
is a collection of items of the same data type, separated by a comma.
与其他数据类型一样, List
也是Haskell中使用的有益数据类型。 按照示例,[a,b,c]是一个字符列表。 因此,根据定义, List
是相同数据类型的项目的集合,用逗号分隔。
Let’s create a list:
让我们创建一个列表:
Prelude> x = [1,2,3,4,5]
Outputs:
输出:
[1,2,3,4,5]
长度 (Length)
Lists also have a couple of methods. Given list x, you can use the following method to get the length:
列表也有两种方法。 给定列表x,可以使用以下方法获取长度:
Prelude> length x
逆转 (Reverse)
Or to reverse a list, you can use:
或反转列表,可以使用:
Prelude> reverse x
加 (Add)
To add an element to a list, you use the ++
operator;
要将元素添加到列表中,请使用++
运算符;
Prelude> x ++ y
The code above combines list x with list y.
上面的代码将列表x与列表y结合在一起。
删除 (Delete)
To delete one single element from a list, you use drop
:
要从列表中删除一个元素,请使用drop
:
Prelude>`drop n xs
元组 (Tuple)
Haskell provides another way to declare multiple values in a single data type. It is known as a tuple. A tuple can be considered as a list. However, there are some technical differences between a tuple and a tist.
Haskell提供了另一种在单个数据类型中声明多个值的方法。 它被称为元组 。 元组可以视为列表。 但是,元组和Tist之间存在一些技术差异。
A tuple has a fixed amount of elements inside it. Tuples are immutable. A Tuple is created as follows:
元组内部具有固定数量的元素。 元组是不可变的。 创建元组如下:
Prelude> (8,16,'b')
Outputs:
输出:
(8,16,'b')
This tuple contains three elements, two numbers, and a character.
该元组包含三个元素,两个数字和一个字符。
Like lists, tuples contain methods with them to determine things like the first or last element in the tuple.
像列表一样,元组包含的方法可以确定元组中的第一个或最后一个元素。
第一要素 (First element)
To retrieve the first element of a tuple, use the following method:
要检索元组的第一个元素,请使用以下方法:
Prelude> fst (16, 8)
This will output:
这将输出:
16
首尾 (Head and tail)
When using the head
method, you also retrieve the first element of a tuple:
使用head
方法时,您还将检索元组的第一个元素:
Prelude> head (16, 8, 24, 32)
Output:
输出:
16
But when using the tail
method, you don’t just take the last or second element of a tuple but all the elements except the first one:
但是,当使用tail
方法时,您不仅要获取元组的最后一个或第二个元素,还要获取除第一个元素之外的所有元素:
Prelude> tail(16, 8, 24, 32)
Outputs:
输出:
[8, 24, 32]
条件语句 (Conditional Statements)
Conditional statements are a feature that allows programmers to apply a condition in the code flow. The programmer can execute a set of instructions depending on a predefined condition.
条件语句是一项功能,允许程序员在代码流中应用条件。 程序员可以根据预定义的条件执行一组指令。
Haskell has the following conditional statements:
Haskell具有以下条件语句:
- If-else statement If-else语句
- Nested if-else statement 嵌套if-else语句
If-else语句 (If-else statement)
The syntax for if
expressions is:
if
表达式的语法为:
if <condition> then <true-value> else <false-value>
功能 (Functions)
Functions play a significant role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its functional definition and declaration.
函数在Haskell中起着重要作用,因为它是一种函数式编程语言。 与其他语言一样,Haskell确实具有其功能定义和声明。
Let’s take a small example of an add
function to understand this concept in detail.
让我们举一个add
函数的小例子来详细了解这个概念。
add :: Integer -> Integer -> Integer --function declaration
add x y = x + y --function definitionmain = do
putStrLn "The addition of the two numbers is:"
print(add 2 5) --calling a function
Output:
输出:
The addition of the two numbers is:
7
模式匹配 (Pattern matching)
Pattern matching is the process of matching a specific type of expression. It is nothing but a technique to simplify your code.
模式匹配是匹配特定类型的表达式的过程。 只是一种简化代码的技术。
Take a look at the following code block.
看一下下面的代码块。
fact :: Int -> Int
fact 0 = 1
fact n = n * fact ( n - 1 ) main = do
putStrLn "The factorial of 4 is:"
print (fact 4)
Here we have used the technique of pattern matching to calculate the factorial of a number.
在这里,我们使用了模式匹配技术来计算数字的阶乘。
守卫 (Guards)
The concept of guards is very similar to pattern matching, but we use guards to test some property of an expression. In the following code, we have modified our factorial program by using the concept of guards:
卫士的概念是非常相似的模式匹配,但是我们用卫士测试表达式的某些属性。 在以下代码中,我们通过使用警卫的概念修改了阶乘程序:
fact :: Integer -> Integer
fact n | n == 0 = 1
| n /= 0 = n * fact (n-1)
main = do
putStrLn "The factorial of 4 is:"
print (fact 4)
递归 (Recursion)
Recursion is a situation where a function calls itself repeatedly. Haskell does not provide any facility of looping any expression more than once.
递归是一种函数反复调用自身的情况。 Haskell不提供任何多次循环任何表达式的功能。
Haskell wants you to break your entire functionality into a collection of different functions and use the recursion technique to implement your functionality.
Haskell希望您将整个功能分解为不同功能的集合,并使用递归技术来实现您的功能。
In the following example, we have used both pattern matching and recursion to calculate the factorial of 4.
在以下示例中,我们同时使用了模式匹配和递归来计算4的阶乘。
fact :: Int -> Int
fact 0 = 1
fact n = n * fact ( n - 1 ) main = do
putStrLn "The factorial of 4 is:"
print (fact 4)
Lambda表达式 (Lambda expression)
A function without having a definition is called a lambda function. A lambda function is denoted by \
character.
没有定义的函数称为lambda函数 。 Lambda函数用\
字符表示。
main = do
putStrLn "The successor of 5 is:"
print ((\x -> x + 1) 5)
模组 (Modules)
If you have worked on Java, then you’ll know how all the classes are bound into a folder called a package. Similarly, Haskell can be considered as a collection of modules.
如果您使用过Java,那么您将知道如何将所有类绑定到一个名为package的文件夹中。 同样,Haskell可被视为模块的集合。
For example, the list module:
例如,列表模块:
import Data.List
You are now able to use the List
functionalities.
现在,您可以使用List
功能。
Some other common modules are:
其他一些常见模块是:
Char
moduleChar
模块Map
moduleMap
模块Set
moduleSet
模块
定制模块 (Custom modules)
Let us create the custom module and define a few functions in it.
让我们创建自定义模块并在其中定义一些功能。
module Custom (
showEven,
showBoolean
) where
showEven:: Int-> Bool
showEven x = do
if x 'rem' 2 == 0
then True
else False
showBoolean :: Bool->Int
showBoolean c = do
if c == True
then 1
else 0
To import it into the program we do:
要将其导入程序,请执行以下操作:
import Custom
main = do
print(showEven 4)
print(showBoolean True)
一些有用的资源: (Some Useful Resources:)
- Official Homepage of Haskell
Haskell官方主页
- Wikipedia Reference for Haskell
维基百科的Haskell参考
结论 (Conclusion)
After reading this article, I hope you can now write a simple Haskell code and have a good idea of what Haskell is about.
阅读本文之后,希望您现在可以编写一个简单的Haskell代码,并对Haskell的用途有一个很好的了解。
I think after learning Haskell, learning other functional programming languages is easier.
我认为在学习Haskell之后,学习其他函数式编程语言会更容易。
翻译自: https://medium.com/better-programming/an-introduction-to-functional-programming-in-haskell-2620eb567c10
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/haskellbc/1903.html