虽然字符串循环 [英] Loop Though Strings

查看:81
本文介绍了虽然字符串循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎被困在试图遍历字符串以查找不在另一个字符串中的字符.该程序的目标是遍历一个字符串并记录不在另一个字符串中的字符.完成所有检查后,将打印出另一个字符串中没有的字符.它们可能不会重复,因此我尝试使用三个循环.

I seem to be stuck trying to loop through strings to find characters that are not in the other string. The goal of the program is to loop though string one and document the characters that are not in the other string. The characters that are not in the other string will be printed out after all the checking is finished. They may not be repeated, hence I attempt to use three loops.

我正在尝试调试以下代码,因为我最终必须相互检查两个字符串,并且我想手动执行此操作以了解具体知识.

I am trying to debug the code below, since I have to eventually check both strings against each other, and I want to do this manually for the know how.

CG-USER(258): (defun stringprod (string1 string2) 
(let ((newString nil))
(let ((letterSearchOn nil))
(loop for i from 0 below (length string1)
    always
    (setf (letterSearchOn (char string1 i))           
         (loop for j from 0 below (length string2)            
             (for ch =  (char string2 j)
             (/when (find ch letterSearchOn :test #'equal)
             (append newString ch)))))))))
STRINGPROD
CG-USER(260): (stringprod "abc" "abc")
Error: (FOR CH = (CHAR STRING2 J)
    (/WHEN (FIND CH LETTERSEARCHON :TEST #'EQUAL)
     (APPEND NEWSTRING CH))) found where LOOP keyword expected.
Current LOOP context: FOR J FROM 0 BELOW (LENGTH STRING2)
   (FOR CH = (CHAR STRING2 J)
    (/WHEN (FIND CH LETTERSEARCHON :TEST #'EQUAL) (APPEND NEWSTRING CH))).
[condition type: PROGRAM-ERROR]
CG-USER(261): 

推荐答案

您需要检查LOOP的语法:

You need to check the syntax of LOOP: http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm

(loop for i from start1
      for j from start2
      do ...)

您的代码添加了括号,缺少了do单词和诸如/之类的怪异字符.

Your code has added parentheses, missing do words and weird characters like /.

如果您的问题是家庭作业,则应将其标记为作业.如果没有,您可以使用SET-DIFFERENCE.

If your question is homework, you should tag it as such. If not you can use SET-DIFFERENCE.

(set-difference (coerce "abc" 'list) (coerce "bcd" 'list))
-> (#\a)

这篇关于虽然字符串循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆