简体   繁体   English

Haskell-基本尾递归

[英]Haskell - Basic Tail Recursion

I have a function that has parameters 我有一个带有参数的函数

whatIndex ::  (Eq a) => a -> [a] -> Integer

where I return the index of a inside [a], starting at 0, or return -1 if it's not found. 我在其中返回内部[a]的索引,从0开始,如果找不到,则返回-1。 This is what I wrote 这就是我写的

module WhatIndex where

whatIndex ::  (Eq a) => a -> [a] -> Integer

whatIndex p [] = -1

whatIndex p (a:as) 
    | p==a = index
    | otherwise = whatIndex p as
    where index = 1+whatIndex p as

Obviously, I'm not correctly increasing index here. 显然,我在这里没有正确增加索引。 Any idea why this isn't working? 知道为什么这行不通吗? Also, I cannot change parameters. 另外,我无法更改参数。

======================== ========================

Here is some basic input/output 这是一些基本的输入/输出

whatIndex 3 [] = -1
whatIndex 2 [1,2,3,2,1]=1
whatIndex 1 [1,2,3,2,1]=0
whatIndex 'b' ['a' .. 'z']=1

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. 1+whatIndex p as将遍历所有剩余列表并计算它们,它不会给您索引。 Just use an iterative recursive helper function like this... 只需使用这样的迭代递归辅助函数...

You can use either a local function, or the lifted version, which is what I have here. 您可以使用本地功能,也可以使用提升版本,这就是我在这里使用的功能。

whatIndex' ::  (Eq a) => Integer -> a -> [a] -> Integer

whatIndex' _ _ [] = -1

whatIndex' i p (x:xs) 
    | p == x = i
    | otherwise = whatIndex' (i+1) p xs

whatIndex p xs = whatIndex' 0 p xs

main = print $ whatIndex 'a' "bbbbaccc"

Here's a non tail-recursive version: 这是一个非尾递归版本:

whatIndex p (x:xs)
    | p == x = 0
    | otherwise = 1 + whatIndex p xs

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. 尾递归是指一类递归函数,其中递归函数中的“最后”或“最终”函数调用是针对函数本身的。 So that means that the function is not calling some other function (like + ) in the "tail position" (the last place where a function call takes place). 因此,这意味着该函数不会在“尾部位置”(发生函数调用的最后一个位置)中调用其他函数(例如+ )。 You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is + . 您可以清楚地看到,在whatIndex的第一个版本中whatIndex的最终函数是whatIndex而在第二个版本(以whatIndex作为参数调用)中调用的最终函数是+

http://en.wikipedia.org/wiki/Tail_call http://en.wikipedia.org/wiki/Tail_call

Edit: here's a version that corresponds more closely with your specification, although it's a bit convoluted and inefficient. 编辑:这是一个与您的规范更加接近的版本,尽管它有点混乱并且效率低下。

whatIndex p xs 
    | not (any (==p) xs) = -1
    | otherwise = whatIndex' p xs where
        whatIndex' p (x:xs)
            | p == x = 0
            | otherwise = 1 + whatIndex' p xs

Try using an accumulator parameter if you want tail recursion: 如果要尾部递归,请尝试使用累加器参数:

whatIndex :: (Eq a) => a -> [a] -> Integer
whatIndex =
  let whatIndex' count p [] = -1
      whatIndex' count p (a:as)
        | p==a = count
        | otherwise = whatIndex' (count + 1) p as
  in whatIndex' 0

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

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