简体   繁体   English

什么“!!”在haskell中意味着什么?

[英]What does “!!” mean in haskell?

There are two functions written in the Haskell wiki website: Haskell wiki网站上有两个函数:

Function 1 功能1

fib = (map fib' [0 ..] !!)
    where
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

Function 2 功能2

fib x = map fib' [0 ..] !! x
    where
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

What does the "!!" 什么是“!!” mean? 意思?

This is actually more difficult to read then it would seem at first as operators in haskell are more generic then in other languages. 这实际上更难以阅读,然后它首先看起来像haskell中的运算符比其他语言更通用。

The first thing that we are all thinking of telling you is to go look this up yourself. 我们都想告诉你的第一件事就是自己去看看。 If you do not already know about hoogle this is the time to become familiar with it. 如果你还不知道hoogle,那么现在是时候熟悉它了。 You can ask it either to tell you what a function does by name or (and this is even more cool) you can give it the type of a function and it can offer suggestions on which function implement that type. 你可以要求它告诉你一个函数通过名称做什么或(这更酷)你可以给它一个函数的类型,它可以提供关于哪个函数实现该类型的建议。

Here is what hoogle tells you about this function (operator): 这是hoogle告诉你的关于这个函数(运算符)的内容:

(!!) :: [a] -> Int -> a

List index (subscript) operator, starting from 0. It is an 
instance of the more general genericIndex, which takes an index     
of any integral type.

Let us assume that you need help reading this. 让我们假设您需要帮助阅读此内容。 The first line tell us that (!!) is a function that takes a list of things ( [a] ) and an Int then gives you back one of the thing in the list ( a ). 第一行告诉我们(!!)是一个函数,它接受一个事物列表( [a] ),然后一个Int然后返回列表中的a东西( a )。 The descriptions tells you what it does. 描述会告诉您它的作用。 It will give you the element of the list indexed by the Int . 它将为您提供由Int索引的列表元素。 So, xs !! i 所以, xs !! i xs !! i works like xs[i] would in Java, C or Ruby. xs !! i工作方式就像Java,C或Ruby中的xs[i]

Now we need to talk about how operators work in haskell. 现在我们需要谈谈运营商如何在haskell中工作。 I'm not going to give you the whole thing here, but I will at least let you know that there is something more here then what you would encounter in other programming languages. 我不打算在这里给你完整的东西,但我至少会告诉你,在这里还有更多东西,然后你将在其他编程语言中遇到什么。 Operators "always" take two arguments and return something ( a -> b -> c ). 运算符“总是”接受两个参数并返回一些东西( a -> b -> c )。 You can use them just like a normal function: 你可以像普通函数一样使用它们:

add x y
(+) x y -- same as above

But, by default, you can also use them between expression (the word for this is 'infix'). 但是,默认情况下,您也可以在表达式之间使用它们(这个词是'中缀')。 You can also make a normal function work like an operator with backtics: 您还可以使正常函数像具有backtics的运算符一样工作:

x + y
x `add` y -- same as above

What makes the first code example you gave (especially for new haskell coders) is that the !! 是什么让你给出的第一个代码示例(特别是对于新的haskell编码器)是!! operator is used as a function rather then in a typical operator (infix) position. 运算符用作函数,而不是典型的运算符(中缀)位置。 Let me add some binding so it is clearer: 让我添加一些绑定,以便更清楚:

-- return the ith Fibonacci number
fib :: Int -> Int  -- (actually more general than this but do't worry about it) 
fib i = fibs !! i
    where
      fibs :: [Int]
      fibs = map fib' [0 ..]

      fib' :: Int -> Int
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

You can now work your way back to example 1. Make sure you understand what map fib' [0 ..] means. 您现在可以回到示例1.确保您了解map fib' [0 ..]含义。

I'm sorry your question got down-voted because if you understood what was going on the answer would have been easy to look up, but if you don't know about operators as the exist in haskell it is very hard to mentally parse the code above. 我很抱歉你的问题被投票了,因为如果你理解了答案的内容很容易查找,但如果你不知道运营商是否存在于haskell,那么很难在精神上解析上面的代码。

(!!) :: [a] -> Int -> a (!!):: [a] - > Int - > a

List index (subscript) operator, starting from 0. It is an instance of the more general genericIndex, which takes an index of any integral type. 列表索引(下标)运算符,从0开始。它是更通用的genericIndex的一个实例,它接受任何整数类型的索引。

See here: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#g:16 见这里: http//www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#g:16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM