简体   繁体   English

用Julia编程语言编写的函数不会在返回时停止

[英]a function written in Julia programming language doesn't stop on return

I'm currently doing the assignment of a subject I'm coursing in university (Programming Languages Theory). 我目前正在做我正在大学中选修的课程(编程语言理论)。 Part of this assignment consists in solving problems that, while are relatively simple to be solved using an imperative/structured approach; 这项工作的一部分在于解决一些问题,这些问题相对比较容易使用命令式/结构化方法解决; are not so trivial when one's using a functional language (like, for example, matrix multiplication). 当使用一种功能语言(例如矩阵乘法)时,它们并不是那么简单。

So, we have 10 problems to solve, and we're allowed to use any functional language (or, at least, a programming language that supports functional programming). 因此,我们要解决10个问题,并且可以使用任何功能语言(或至少使用支持功能编程的编程语言)。 So, while I'm really familiar with Haskell and Miranda, I was learning by myself how to program in Julia, therefore I chose it as the programming language for this job. 因此,虽然我非常熟悉Haskell和Miranda,但我自己学习如何在Julia中编程,因此我选择了它作为这项工作的编程语言。

One of the problems I have to solve is to create a program that, given a string "S" as input, it returns "true" if the string contains any substring that's palindrome and whose length is greater (or equal) than 3. 我要解决的问题之一是创建一个程序,给定字符串“ S”作为输入,如果该字符串包含回文且长度大于(或等于)3的任何子字符串,则返回“ true”。

So, I used a "brute-force recursive" approach: test if the input string S is palindrome, if it is, return true, otherwise recursively check if S without its first character or without its last character is palindrome. 因此,我使用了一种“强力递归”方法:测试输入字符串S是否是回文,如果是,则返回true,否则,递归检查没有第一个字符或没有最后一个字符的S是否是回文。 If there's no palindrome substring greater or equal than 3, return false 如果没有回文子串大于或等于3,则返回false

This will create a recursion tree that checks all possible substrings in S. Even though this algorithm is exponential , it does the job. 这将创建一个递归树,用于检查S中所有可能的子字符串。即使此算法是指数算法,它也能完成任务。 Also, I can't use dynamic programming, divide-and-conquer, memoization, efficient data structures or any " dank algorithm design technix " - so, I have to stick with the pure functional, recursive approach. 另外,我不能使用动态编程,分而治之,备忘录,有效的数据结构或任何“ 笨拙的算法设计技术 ”-因此,我必须坚持使用纯函数递归方法。

Here's the code: 这是代码:

function SubPalin(S)
  if length(S) > 2
    if palindromeChecker(S) == true
      return true
    else
      SubPalin(S[2:end])
      SubPalin(S[1:end-1])
    end
  else
    return false
  end
end

Note: palindromeChecker is a function that checks if a substring is palindrome (yeah, obvious, I know) 注意:palindromeChecker是一个检查子字符串是否为回文的函数(是的,很明显,我知道)

The problem is: while this code do check all possible substrings greater than 3, it doesn't halt when it finds a palindrome substring. 问题是:尽管此代码确实检查了所有可能大于3的子字符串,但在找到回文子字符串时并没有停止。 What's even weird is that, after some tests, I found out that the code doesn't stop even when it returns true. 甚至很奇怪的是,经过一些测试,我发现即使返回true,代码也不会停止。 It just keeps going till it finds the recursion base case. 它一直持续到找到递归基本案例为止。

So, what's happening? 那么,发生了什么事? I'm just dumb and I can't see the bug or there's something wrong with the language? 我只是傻瓜,看不到错误或语言有问题?

You may want to consider something like this line of code in your first else branch? 您可能要在您的第一个else分支中考虑类似以下代码行的内容? return SubPalin(S[2:end]) || SubPalin(S[1:end-1])

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

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