使用字符串"ABCDEFGHIJKLMNOPQRSTUVWXYZ“。最初从字母表的索引"0“开始,我将跟踪每次"planchette”向左或向右移动的时间。如果星盘悬停,我就会记录下那封信。我将在函数中使用string-length、string-ref和list->string。
(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“
发布于 2014-03-08 03:51:27
我会选择这样的东西:
(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)))或
(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))))发布于 2014-03-09 00:14:06
以下是实现图灵机磁带时常用的另一种方法:
将alphabet转换为list,然后去掉car和cdr,将列表分为三个部分:left、center和right;从而解压列表。这些操作以与列表拉链相同的方式影响列表。
请注意,没有索引或偶数。
(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)))))发布于 2014-03-08 23:35:22
不要在遍历“悬停”列表时处理字母表;相反,可以将“悬停”列表看作是根据left ->减一和right ->加一生成索引。这样,顶层函数将是:
(define ouija
(lambda (actions alphabet)
(list->string (map (lambda (index) (string-ref alphabet index))
(actions->indices actions 0)))))现在,遍历在添加或减去之后创建索引列表的操作。
(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)然后最后:
> (ouija '(hover right right hover) "ABCDEF")
"AC"https://stackoverflow.com/questions/22259144
复制相似问题