首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方案:如何创建简单的“占卜板”(字符串过程)

方案:如何创建简单的“占卜板”(字符串过程)
EN

Stack Overflow用户
提问于 2014-03-08 03:36:33
回答 3查看 125关注 0票数 0

使用字符串"ABCDEFGHIJKLMNOPQRSTUVWXYZ“。最初从字母表的索引"0“开始,我将跟踪每次"planchette”向左或向右移动的时间。如果星盘悬停,我就会记录下那封信。我将在函数中使用string-length、string-ref和list->string。

代码语言:javascript
复制
(define alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ")

(trace-define ouija
  (lambda (ls1 ls2)
    (ouija-help ls1 alphabet 0)))

(trace-define ouija-help
  (lambda (ls1 ls2 x)
    (cond
      [(and (equal? (car ls1) 'left) (equal? (string-ref ls2 x) 'a)) (list->string (cons 'a (ouija-help (cdr ls1) ls2 x)))]
      [(and (equal? (car ls1) 'right) (equal? (string-ref ls2 x) 'z)) (list->string (cons 'z (ouija-help (cdr ls1) ls2 x)))]
      [(equal? (car ls1) 'right) (string-ref (string-ref ls2 x) (+ x 1))]
      [(equal? (car ls1) 'left) (string-ref (string-ref ls2 x) (+ x 1))]
      [(equal? (car ls1) 'hover) (list->string (cons (string-ref ls2 x) (ouija-help (cdr ls1) ls2 x)))]
      )))

正确输入/输出的示例:

~ (ouija '()字母表)

"“

~(ouija‘(悬停)字母表)

"A“

~(ouija‘(鼠标右悬停)字母表)

"BBBBB“

~(ouija '(hover right hover right hover)字母)

"ABC“

~(ouija‘(右悬停左悬停左悬停右悬停)字母表)

"DCBC“

EN

回答 3

Stack Overflow用户

发布于 2014-03-08 03:51:27

我会选择这样的东西:

代码语言:javascript
复制
(define (ouija actions board)
  (define imax (- (string-length board) 1))
  (define (helper actions i)
    (if (null? actions)
        '()
        (case (car actions)
          ((hover) (cons (string-ref board i) (helper (cdr actions) i)))
          ((left)  (helper (cdr actions) (if (> i 0) (- i 1) i)))
          ((right) (helper (cdr actions) (if (< i imax) (+ i 1) i))))))
  (list->string (helper actions 0)))

代码语言:javascript
复制
(define helper
  (lambda (actions board i)
    (if (null? actions)
        '()
        (case (car actions)
          ((hover) (cons (string-ref board i) (helper (cdr actions) board i)))
          ((left)  (helper (cdr actions) board (if (> i 0) (- i 1) i)))
          ((right) (helper (cdr actions) board (if (< i (- (string-length board) 1)) (+ i 1) i)))))))

(define ouija
  (lambda (actions board)
    (list->string (helper actions board 0))))
票数 1
EN

Stack Overflow用户

发布于 2014-03-09 00:14:06

以下是实现图灵机磁带时常用的另一种方法:

alphabet转换为list,然后去掉carcdr,将列表分为三个部分:leftcenterright;从而解压列表。这些操作以与列表拉链相同的方式影响列表。

请注意,没有索引或偶数。

代码语言:javascript
复制
(define (ouija-helper actions left center right)
  (if (null? actions) 
      '()
      (case (car actions)
        ((hover) (cons center (ouija-helper (cdr actions) left center right)))
        ((left) (if (null? left) 
                    (ouija-helper (cdr actions) left center right)
                    (ouija-helper (cdr actions) (cdr left) (car left) (cons center right))))
        ((right) (if (null? right) 
                     (ouija-helper (cdr actions) left center right)
                     (ouija-helper (cdr actions) (cons center left) (car right) (cdr right)))))))

(define (ouija actions alphabet) 
  (let ((alphabet (string->list alphabet)))
    (list->string (ouija-helper actions '() (car alphabet) (cdr alphabet)))))
票数 1
EN

Stack Overflow用户

发布于 2014-03-08 23:35:22

不要在遍历“悬停”列表时处理字母表;相反,可以将“悬停”列表看作是根据left ->减一和right ->加一生成索引。这样,顶层函数将是:

代码语言:javascript
复制
(define ouija
  (lambda (actions alphabet)
     (list->string (map (lambda (index) (string-ref alphabet index))
                        (actions->indices actions 0)))))

现在,遍历在添加或减去之后创建索引列表的操作。

代码语言:javascript
复制
(define actions->indices
  (lambda (actions index)
    (if (null? actions)
        '()
        (let ((rest (cdr actions)))
          (case (car actions)
            ((hover) (cons index (actions->indices rest index)))
            ((left ) (actions->indices rest (- index 1)))
            ((right) (actions->indices rest (+ index 1))))))))


> (actions->indices '(hover hover) 0)
(0 0)
> (actions->indices '(hover right left hover) 0)
(0 0)
> (actions->indices '(right right hover right right hover) 0)
(2 4)

然后最后:

代码语言:javascript
复制
> (ouija '(hover right right hover) "ABCDEF")
"AC"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22259144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档