Functional programming 如何在racket中创建一个否定二进制函数
我对如何处理这个函数感到困惑 函数negate binary消耗一个自然数列表、alist和products 包含列表中有效二进制数的补数的列表 (即仅包含0和1的数字) 生成的列表中的“1”的补码必须 与列表中显示的原始二进制数的相对顺序相同 假设所有数字都没有前导0;换句话说,列表将不包含任何像0001这样的数字Functional programming 如何在racket中创建一个否定二进制函数,functional-programming,scheme,racket,Functional Programming,Scheme,Racket,我对如何处理这个函数感到困惑 函数negate binary消耗一个自然数列表、alist和products 包含列表中有效二进制数的补数的列表 (即仅包含0和1的数字) 生成的列表中的“1”的补码必须 与列表中显示的原始二进制数的相对顺序相同 假设所有数字都没有前导0;换句话说,列表将不包含任何像0001这样的数字 数字->字符串和字符串->数字可能会有所帮助* 编辑:删除所有高阶列表抽象,如映射、和映射、过滤器和应用 #lang racket ; 1String is a String
- 数字->字符串和字符串->数字可能会有所帮助*
映射
、和映射
、过滤器
和应用
#lang racket
; 1String is a String of length 1.
; [List-of Number] -> [List-of Number]
(define (negate-binary lon)
(ones-complement* (binary-only lon)))
; [List-of Number] -> [List-of Number]
; only keep the binary numbers
(define (binary-only lon)
(cond
[(empty? lon) '()]
[else (if (is-binary? (first lon))
(cons (first lon) (binary-only (rest lon)))
(binary-only (rest lon)))]))
; [List-of Number] -> [List-of Number]
; one's complement for all numbers in lon
(define (ones-complement* lon)
(cond
[(empty? lon) '()]
[else (cons (ones-complement (first lon))
(ones-complement* (rest lon)))]))
; Number -> Boolean
; is `num` a binary number?
(define (is-binary? num)
(all-binary-1strings? (num->1string-list num)))
; [List-of 1String] -> Boolean
; are all 1strings in lo1s binary?
(define (all-binary-1strings? lo1s)
(cond
[(empty? lo1s) #true]
[else (and (is-binary-1string? (first lo1s))
(all-binary-1strings? (rest lo1s)))]))
; Number -> [List-of 1String]
; number to a list of 1strings
(define (num->1string-list num)
(explode (number->string num)))
; String -> [List-of 1String]
; convert a string to a list of 1strings
(define (explode str)
(charlist->string-list (string->list str)))
; [List-of Char] -> [List-of 1String]
; convert a list of characters to a list of 1strings
(define (charlist->string-list charlist)
(cond
[(empty? charlist) '()]
[else (cons (string (first charlist))
(charlist->string-list (rest charlist)))]))
; 1String -> Boolean
; is 1string a binary 1string?
(define (is-binary-1string? 1string)
(or (string=? 1string "0") (string=? 1string "1")))
; Number -> Number
; one's complement of a number
(define (ones-complement num)
(str-list->number (flip-bits (num->1string-list num))))
; [List-of 1String] -> [List-of 1String]
; "0" to "1" and "1" to "0" in list of 1strings
(define (flip-bits lo1s)
(flip-all-bits lo1s))
; [List-of 1String] -> [List-of 1String]
; flip bits of all 1strings in lo1s
(define (flip-all-bits lo1s)
(cond
[(empty? lo1s) '()]
[else (cons (flip-bit (first lo1s))
(flip-all-bits (rest lo1s)))]))
; 1String -> 1String
; convert "0" to "1" and "1" to "0"
(define (flip-bit 1string)
(if (string=? 1string "0") "1" "0"))
; [List-of 1String] -> Number
; convert the list of 1strings to a number
(define (str-list->number lo1s)
(string->number (join-all lo1s)))
; [List-of 1String] -> String
; join all 1strings in lo1s into a single string
(define (join-all lo1s)
(cond
[(empty? lo1s) ""]
[else (string-append (first lo1s)
(join-all (rest lo1s)))]))
(module+ test
(require rackunit)
(check-equal? (negate-binary (list 1000 101010 123 111)) (list 111 10101 0))
(check-equal? (negate-binary (list 95 137 401)) empty))
嘿,阿萨瓦,非常感谢。但是,我们不允许使用抽象函数,如map等。只允许使用基本函数Shi loren,我已经更新了答案,删除了所有非基本函数。你好,谢谢Atharva。只是一个简单的问题,我有没有办法简化这一切。我相信有很多助手函数。此外,我们不能仅使用if语句cond。将ifs转换为conds很简单,可以通过内联一些函数进一步简化,如
翻转位
,分解
,str list->number
,当我试图将ifs简化为conds时,它会给我一个错误。
#lang racket
; 1String is a String of length 1.
; [List-of Number] -> [List-of Number]
(define (negate-binary lon)
(map ones-complement (filter is-binary? lon)))
; Number -> Boolean
; is `num` a binary number?
(define (is-binary? num)
(andmap is-binary-1string? (num->1string-list num)))
; Number -> [List-of 1String]
; number to a list of 1strings
(define (num->1string-list num)
(explode (number->string num)))
; String -> [List-of 1String]
; convert a string to a list of 1strings
(define (explode str)
(map string (string->list str)))
; 1String -> Boolean
; is 1string a binary 1string?
(define (is-binary-1string? 1string)
(or (string=? 1string "0") (string=? 1string "1")))
; Number -> Number
; one's complement of a number
(define (ones-complement num)
(str-list->number (flip-bits (num->1string-list num))))
; [List-of 1String] -> [List-of 1String]
; "0" to "1" and "1" to "0" in list of 1strings
(define (flip-bits lo1s)
(map flip-bit lo1s))
; 1String -> 1String
; convert "0" to "1" and "1" to "0"
(define (flip-bit 1string)
(if (string=? 1string "0") "1" "0"))
; [List-of 1String] -> Number
; convert the list of 1strings to a number
(define (str-list->number lo1s)
(string->number (apply string-append lo1s)))
(module+ test
(require rackunit)
(check-equal? (negate-binary (list 1000 101010 123 111)) (list 111 10101 0))
(check-equal? (negate-binary (list 95 137 401)) empty))
#lang racket
; 1String is a String of length 1.
; [List-of Number] -> [List-of Number]
(define (negate-binary lon)
(ones-complement* (binary-only lon)))
; [List-of Number] -> [List-of Number]
; only keep the binary numbers
(define (binary-only lon)
(cond
[(empty? lon) '()]
[else (if (is-binary? (first lon))
(cons (first lon) (binary-only (rest lon)))
(binary-only (rest lon)))]))
; [List-of Number] -> [List-of Number]
; one's complement for all numbers in lon
(define (ones-complement* lon)
(cond
[(empty? lon) '()]
[else (cons (ones-complement (first lon))
(ones-complement* (rest lon)))]))
; Number -> Boolean
; is `num` a binary number?
(define (is-binary? num)
(all-binary-1strings? (num->1string-list num)))
; [List-of 1String] -> Boolean
; are all 1strings in lo1s binary?
(define (all-binary-1strings? lo1s)
(cond
[(empty? lo1s) #true]
[else (and (is-binary-1string? (first lo1s))
(all-binary-1strings? (rest lo1s)))]))
; Number -> [List-of 1String]
; number to a list of 1strings
(define (num->1string-list num)
(explode (number->string num)))
; String -> [List-of 1String]
; convert a string to a list of 1strings
(define (explode str)
(charlist->string-list (string->list str)))
; [List-of Char] -> [List-of 1String]
; convert a list of characters to a list of 1strings
(define (charlist->string-list charlist)
(cond
[(empty? charlist) '()]
[else (cons (string (first charlist))
(charlist->string-list (rest charlist)))]))
; 1String -> Boolean
; is 1string a binary 1string?
(define (is-binary-1string? 1string)
(or (string=? 1string "0") (string=? 1string "1")))
; Number -> Number
; one's complement of a number
(define (ones-complement num)
(str-list->number (flip-bits (num->1string-list num))))
; [List-of 1String] -> [List-of 1String]
; "0" to "1" and "1" to "0" in list of 1strings
(define (flip-bits lo1s)
(flip-all-bits lo1s))
; [List-of 1String] -> [List-of 1String]
; flip bits of all 1strings in lo1s
(define (flip-all-bits lo1s)
(cond
[(empty? lo1s) '()]
[else (cons (flip-bit (first lo1s))
(flip-all-bits (rest lo1s)))]))
; 1String -> 1String
; convert "0" to "1" and "1" to "0"
(define (flip-bit 1string)
(if (string=? 1string "0") "1" "0"))
; [List-of 1String] -> Number
; convert the list of 1strings to a number
(define (str-list->number lo1s)
(string->number (join-all lo1s)))
; [List-of 1String] -> String
; join all 1strings in lo1s into a single string
(define (join-all lo1s)
(cond
[(empty? lo1s) ""]
[else (string-append (first lo1s)
(join-all (rest lo1s)))]))
(module+ test
(require rackunit)
(check-equal? (negate-binary (list 1000 101010 123 111)) (list 111 10101 0))
(check-equal? (negate-binary (list 95 137 401)) empty))