“在过程cadr中:错误类型(期望对):#f”

huangapple go评论132阅读模式
英文:

Scheme word translater "In procedure cadr: Wrong type (expecting pair): #f"

问题

Sure, here's the translated code part:

(define data '((hallo hello)
               (apfel apple)
               (welt world)))

(map (lambda (i) (display (cadr (assoc i data)))) (list "hallo" "apfel" "welt"))

Translation:

(define data '((hallo hello)
               (apfel apple)
               (welt world)))

(map (lambda (i) (display (cadr (assoc i data)))) (list "hallo" "apfel" "welt"))

If you have any more code or text that needs translation, feel free to ask.

英文:

I want to build a word translater in Scheme. I have to use Scheme only.
So far the base of my code looks like this:

(define data '((hallo hello)
               (apfel apple)
               (welt world)))


(map (lambda (i) (display (cadr (assoc i data)))) (list "hallo" "apfel" "welt"))

After I tried to run it the error "In procedure cadr: Wrong type (expecting pair): #f" occured.

It should display the translation of each element of the list data.

Thank you for helping me!

答案1

得分: 3

你的字典是一个符号列表,但你的输入是一个字符串列表 - 它们是不同的数据类型,所以 assoc 找不到它们。 你应该考虑单词未找到的情况(assoc 返回 #f,这解释了你遇到的错误)。 不要使用 display 来返回列表中的结果。 试试这个开始:

(define data '((hallo hello)
               (apfel apple)
               (welt world)))

(map (lambda (i) (cadr (assoc i data)))
     '(hallo apfel welt))
=> '(hello apple world)
英文:

Your dictionary is a list of symbols, but your input is a list of strings - they're different data types, so assoc doesn't find them. You should consider the case when the word is not found (assoc returns #f, this explains the error you're getting). And don't use display to return results in a list. Try this for starters:

(define data '((hallo hello)
               (apfel apple)
               (welt world)))

(map (lambda (i) (cadr (assoc i data)))
     '(hallo apfel welt))
=> '(hello apple world)

答案2

得分: 0

你可以注意到 `(assoc bad-value alist)` 返回 `#f` 并尝试调用 `(cadr #f)` 会破坏你的程序。让我们尝试对你的程序进行一些改进。首先,你需要使用 `reduce` 函数:

```(define (reduce fn init-value lst)
  (if (null? lst) init-value
      (fn (car lst)
          (reduce fn init-value (cdr lst)))))

保存它到你的 utils.scm,你将需要它每天来工作、教学等。现在你可以对你的数据 alist 进行折叠/归约操作:

(define *data*
  '( (hallo hello)
     (apfel apple)
     (welt world) ))

(reduce (λ (item acc)
          (let ((found-pair (assoc item *data*)))
            (if found-pair
                (cons (cadr found-pair) acc)
                acc)))
        '()
        '(hatllo ; bad value
          apfel  ; good value
          welt   ; good value
          ))
;; 结果 ==> '(apple world)

它运行得很好,但是!这还不够。完美的话,你需要从 data 中移除每个找到的值,但不破坏 data。总之,上面的解决方案是一个快速原型。采用这个思路并进行改进。


<details>
<summary>英文:</summary>

You could take a note that `(assoc bad-value alist)` returns `#f` and trying to call `(cadr #f)` breaking down your program. Let&#39;s try to add some improvements to your program. At first step you need `reduce` function:
```(define (reduce fn init-value lst)
  (if (null? lst) init-value
      (fn (car lst)
          (reduce fn init-value (cdr lst)))))

save it to your utils.scm, you will need it every day to work, teach and so on. And now you can just fold/reduce yout data alist:

(define *data*
  &#39;((hallo hello)
    (apfel apple)
    (welt world)))

(reduce (λ (item acc)
          (let ((found-pair (assoc item *data*)))
            (if found-pair
                (cons (cadr found-pair) acc)
                acc)))
        &#39;()
        &#39;(hatllo ; bad value
          apfel  ; good value
          welt   ; good value
          ))
;; result ==&gt; &#39;(apple world)

It works fine, but! It's not enough. Perfectly you need to remove every founded value from data but not destroy the data. Conclusing, the solution above is fast prototype. Take this idea and improve it.

huangapple
  • 本文由 发表于 2023年3月9日 22:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定