【问题标题】:Andmap\ormap - chez schemeAndmap\ormap - chez 方案
【发布时间】:2012-01-04 02:34:21
【问题描述】:

我试图在 chez 方案中查找有关 andmap 和 ormap 操作的信息。

还是不明白这些操作的用途,和map有什么区别。

【问题讨论】:

  • 所有这些功能都在 Chez Scheme 用户指南中有详细记录。

标签: scheme chez-scheme


【解决方案1】:

在伪方案中,

(andmap f xs)  ==  (fold and #t (map f xs))
(ormap  f xs)  ==  (fold or  #f (map f xs))

除了:

  1. 你不能这样使用andor
  2. andmapormap 可以对列表进行短路处理。

也就是说,除了短路行为略有不同,

(andmap f (list x1 x2 x3 ...))  ==  (and (f x1) (f x2) (f x3) ...)
(ormap  f (list x1 x2 x3 ...))  ==  (or  (f x1) (f x2) (f x3) ...)

【讨论】:

    【解决方案2】:
    Petite Chez Scheme Version 8.3
    Copyright (c) 1985-2011 Cadence Research Systems
    
    > (define (andmap f xs)
        (cond ((null? xs) #t)
              ((f (car xs))
                (andmap f (cdr xs)))
              (else #f)))
    > (define (ormap f xs)
        (cond ((null? xs) #f)
              ((f (car xs)) #t)
              (else (ormap f (cdr xs)))))
    > (andmap even? '(2 4 6 8 10))
    #t
    > (andmap even? '(2 4 5 6 8))
    #f
    > (ormap odd? '(2 4 6 8 10))
    #f
    > (ormap odd? '(2 4 5 6 8))
    #t
    

    【讨论】:

      【解决方案3】:

      由实用方案网提供:

      (ormap procedure list1 list2 ...)
      

      procedure依次应用于列表的相应元素,直到列表用完或过程返回真值。

      (andmap procedure list1 list2 ...)
      

      procedure依次应用于列表的相应元素,直到列表用完或过程返回错误值。

      http://practical-scheme.net/wiliki/schemexref.cgi/ormap

      认为这值得放在这里,因为这个 StackOverflow 问题是 Google 上的第一个结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-30
        • 2020-07-04
        • 1970-01-01
        • 2013-11-06
        • 2023-03-10
        • 1970-01-01
        • 2021-03-24
        • 1970-01-01
        相关资源
        最近更新 更多