Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Racket 球拍-从列表中添加或删除元素_Racket - Fatal编程技术网

Racket 球拍-从列表中添加或删除元素

Racket 球拍-从列表中添加或删除元素,racket,Racket,我正在做一个问题,它要求我编写一个函数,该函数使用 列表(dict1)和包含操作(删除或添加)和值的列表(act)。更新应符合以下规范: •如果act的第一个元素是“删除”,并且键在字典中,则应删除关联列表中的匹配值 •如果匹配值恰好是关联列表中的最后一个元素,则键应保留在字典中,但关联列表变为空。如果值不在字典中,则字典不会更改 •如果act的值为“add”,且键在字典中,则新值 如果关联列表不存在,则应将其添加到字典中的关联列表中。如果该值已存在于关联列表中,则更新的字典不会更改 •如果ac

我正在做一个问题,它要求我编写一个函数,该函数使用 列表(dict1)和包含操作(删除或添加)和值的列表(act)。更新应符合以下规范:

•如果act的第一个元素是“删除”,并且键在字典中,则应删除关联列表中的匹配值

•如果匹配值恰好是关联列表中的最后一个元素,则键应保留在字典中,但关联列表变为空。如果值不在字典中,则字典不会更改

•如果act的值为“add”,且键在字典中,则新值 如果关联列表不存在,则应将其添加到字典中的关联列表中。如果该值已存在于关联列表中,则更新的字典不会更改

•如果act的值为“add”,且字典中不存在键,则 应将密钥的新关联列表条目添加到字典中

例如:

(define dict1 (list (list "Num" (list 5 1.3 -1))
 (list "Char" (list #\p))
 (list "Str"
 (list "cs" "CS" "Computer Science"))))
然后

我只能提出第一步,以下是我迄今为止所做的:

(define (update dict1 act)
  (cond
    [(equal? (first act) "remove") (remove-value dict1 (second act))]
    [(equal? (first act) "add") (add-value dict1 (second act))]))
对于这个问题,我只允许使用
成员?
删除

我知道在过去的几天里我问了很多问题,但我对球拍还不熟悉,我正在尽我最大的努力去学习它:(请帮助我)没有设置!没有本地版本

; please tell us which language you use (in front of code)

#|

In Beginning Student Language variabe can't be function
error: function call: expected a function after the open parenthesis, but found a variable

(define (f fn) (fn 1))
(f add1)
|#
; use "Advanced Student" language or "#lang racket".
#lang racket

(define dict1
  (list (list "Num" (list 1 2 3))
        (list "Char" (list #\p))
        (list "Bool" '())
        (list "Str" (list "a" "b" "c"))))

(define (index-title action)
  (cond
    [(number? (second action)) 0]
    [(char? (second action)) 1]
    [(boolean? (second action)) 2]
    [(string? (second action)) 3]
    [else (error "type error")]))


(define (my-remove word lst)
  (cond
    [(empty? lst) empty]
    [(equal? word (first lst))
     (rest lst)]
    [else
     (cons (first lst)
           (my-remove word (rest lst)))]))


(define (exist? word lst)
  (cond
    [(empty? lst) #f]
    [(equal? word (first lst)) #t]
    [else
     (exist? word (rest lst))]))

(define (add-if-not-exist word data)
  (if (exist? word data)
      data
      (cons word data)))

(define (action-to-data word data action)
  (cond
    [(equal? action "add")
     (add-if-not-exist word data)]
    [(equal? action "remove")
     (my-remove word data)]))


(define (aux-build-list i n dict act a-function)
  (cond
    [(= i n) empty]
    [else
     (cons (a-function i dict act)
           (aux-build-list (+ i 1) n dict act a-function))]))


(define (my-build-list n dict act a-function)
  (aux-build-list 0 n dict act a-function))


(define (build-new-sub-dict n dict act)
  (cond
    [(= n (index-title act))
     (list (first (list-ref dict n))
           (action-to-data (second act) (second (my-list-ref dict n)) (first act)))]
    [else
     (list-ref dict n)]))


(define (my-list-ref lst n)
  (cond
    [(< n 0) (error "error")]
    [(= n 0) (first lst)]
    [else
     (my-list-ref (rest lst) (- n 1))]))


(define (update dict act)
  (set! dict1
        (my-build-list (length dict)
                       dict
                       act
                       (lambda (n dict act) (build-new-sub-dict n dict act)))))

;;; Test
(update dict1 '("add" 2))
(update dict1 '("remove" 3))
(update dict1 '("add" 4))
(update dict1 '("add" 4))
(update dict1 '("remove" 1))
(update dict1 '("remove" 2))
dict1
;请告诉我们您使用哪种语言(在代码前面)
#|
在一开始,学生语言变量不能起作用
错误:函数调用:应为开括号后的函数,但找到了变量
(定义(f fn)(fn 1))
(f增补1)
|#
使用“高级学生”语言或“朗朗球拍”。
#朗格球拍
(1)
(列表(列表“Num”(列表1、2、3))
(列表“字符”(列表#\p))
(列出“Bool”()
(列表“Str”(列表“a”“b”“c”))
(定义(索引标题操作)
(续)
[(第二个动作)0]
[(字符(第二个动作))1]
[(布尔?(第二个动作))2]
[(字符串?(第二个动作))3]
[其他(错误“类型错误”)])
(定义(删除单词lst)
(续)
[(空?lst)空]
[(相等字(第一个lst))
(其余第1阶段)]
[其他
(反对(第一轮)
(我的删除字(rest lst)))]
(定义(存在?单词lst)
(续)
[(空?lst)#f]
[(相等字(第一个lst))#t]
[其他
(存在?单词(rest lst))))
(定义(如果不存在单词数据,则添加)
(如果(存在?字数据)
数据
(单词数据)
(定义(操作到数据字数据操作)
(续)
[(相等?作用“添加”)
(如果不存在单词数据,则添加)]
[(同等作用“移除”)
(我的删除文字数据)])
(定义(dict act a功能中的辅助构建列表)
(续)
[(=in)空的]
[其他
(cons(职能i指令法案)
(辅助构建列表(+i 1)n指令行为a-功能)]]
(定义(我的构建列表n dict act a函数)
(辅助构建列表0 n指令动作a-功能)
(定义(建立新的子目录n目录法案)
(续)
[(=n(索引标题法))
(列表(第一个(列表参考目录n))
(对数据的操作(第二幕)(第二幕(我的列表参考dict n))(第一幕))]
[其他
(列表参考目录n)])
(定义(我的列表参考lst n)
(续)
[(
没有设置!没有本地版本

; please tell us which language you use (in front of code)

#|

In Beginning Student Language variabe can't be function
error: function call: expected a function after the open parenthesis, but found a variable

(define (f fn) (fn 1))
(f add1)
|#
; use "Advanced Student" language or "#lang racket".
#lang racket

(define dict1
  (list (list "Num" (list 1 2 3))
        (list "Char" (list #\p))
        (list "Bool" '())
        (list "Str" (list "a" "b" "c"))))

(define (index-title action)
  (cond
    [(number? (second action)) 0]
    [(char? (second action)) 1]
    [(boolean? (second action)) 2]
    [(string? (second action)) 3]
    [else (error "type error")]))


(define (my-remove word lst)
  (cond
    [(empty? lst) empty]
    [(equal? word (first lst))
     (rest lst)]
    [else
     (cons (first lst)
           (my-remove word (rest lst)))]))


(define (exist? word lst)
  (cond
    [(empty? lst) #f]
    [(equal? word (first lst)) #t]
    [else
     (exist? word (rest lst))]))

(define (add-if-not-exist word data)
  (if (exist? word data)
      data
      (cons word data)))

(define (action-to-data word data action)
  (cond
    [(equal? action "add")
     (add-if-not-exist word data)]
    [(equal? action "remove")
     (my-remove word data)]))


(define (aux-build-list i n dict act a-function)
  (cond
    [(= i n) empty]
    [else
     (cons (a-function i dict act)
           (aux-build-list (+ i 1) n dict act a-function))]))


(define (my-build-list n dict act a-function)
  (aux-build-list 0 n dict act a-function))


(define (build-new-sub-dict n dict act)
  (cond
    [(= n (index-title act))
     (list (first (list-ref dict n))
           (action-to-data (second act) (second (my-list-ref dict n)) (first act)))]
    [else
     (list-ref dict n)]))


(define (my-list-ref lst n)
  (cond
    [(< n 0) (error "error")]
    [(= n 0) (first lst)]
    [else
     (my-list-ref (rest lst) (- n 1))]))


(define (update dict act)
  (set! dict1
        (my-build-list (length dict)
                       dict
                       act
                       (lambda (n dict act) (build-new-sub-dict n dict act)))))

;;; Test
(update dict1 '("add" 2))
(update dict1 '("remove" 3))
(update dict1 '("add" 4))
(update dict1 '("add" 4))
(update dict1 '("remove" 1))
(update dict1 '("remove" 2))
dict1
;请告诉我们您使用哪种语言(在代码前面)
#|
在一开始,学生语言变量不能起作用
错误:函数调用:应为开括号后的函数,但找到了变量
(定义(f fn)(fn 1))
(f增补1)
|#
使用“高级学生”语言或“朗朗球拍”。
#朗格球拍
(1)
(列表(列表“Num”(列表1、2、3))
(列表“字符”(列表#\p))
(列出“Bool”()
(列表“Str”(列表“a”“b”“c”))
(定义(索引标题操作)
(续)
[(第二个动作)0]
[(字符(第二个动作))1]
[(布尔?(第二个动作))2]
[(字符串?(第二个动作))3]
[其他(错误“类型错误”)])
(定义(删除单词lst)
(续)
[(空?lst)空]
[(相等字(第一个lst))
(其余第1阶段)]
[其他
(反对(第一轮)
(我的删除字(rest lst)))]
(定义(存在?单词lst)
(续)
[(空?lst)#f]
[(相等字(第一个lst))#t]
[其他
(存在?单词(rest lst))))
(定义(如果不存在单词数据,则添加)
(如果(存在?字数据)
数据
(单词数据)
(定义(操作到数据字数据操作)
(续)
[(相等?作用“添加”)
(如果不存在单词数据,则添加)]
[(同等作用“移除”)
(我的删除文字数据)])
(定义(dict act a功能中的辅助构建列表)
(续)
[(=in)空的]
[其他
(cons(职能i指令法案)
(辅助构建列表(+i 1)n指令行为a-功能)]]
(定义(我的构建列表n dict act a函数)
(辅助构建列表0 n指令动作a-功能)
(定义(建立新的子目录n目录法案)
(续)
[(=n(索引标题法))
(列表(第一个(列表参考目录n))
(对数据的操作(第二幕)(第二幕(我的列表参考dict n))(第一幕))]
[其他
(列表参考目录n)])
(定义(我的列表参考lst n)
(续)
[(