【问题标题】:Scheme function to determine whether the list follows a pattern [duplicate]确定列表是否遵循模式的方案函数[重复]
【发布时间】:2013-10-18 02:07:02
【问题描述】:

方案:确定传入列表是否遵循 A B 模式的函数。 AB 模式将是 (A B A B A B) 或 (A B) 或 A 和 B 的任意组合重复。 A 和 B 不是变量,我的字面意思是字母 A 后跟字母 B。

【问题讨论】:

  • 当输入为'(A B A)时它应该评估什么?

标签: scheme


【解决方案1】:

一种天真的但简单的算法:寻找列表头部的模式;如果匹配则返回 true,如果它不在列表的其余部分中查找该模式。

(define (sublist? pattern list)
  (and (not (null? list))
       (or (match? pattern list)
           (sublist? pattern (cdr list)))))

match? 函数可以是:

(define (match? p l)
  (or (null? p)
      (and (not (null? l))
           (eq?    (car p) (car l))
           (match? (cdr p) (cdr l)))))

【讨论】:

  • 请提供“已回答”的支票和投票(如果可以的话)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多