s c h e m a t i c s : c o o k b o o k

/ WebHome / TOC / NumberChapter / Cookbook.NumberRecipeCorrectPlurals

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Printing Correct Plurals

Problem

You would like to generate messages reporting quantities with correct grammer. For example, you would like to say "There is 1 monster approaching" or "There are 2 monsters approaching", not "There are 1 monsters approaching."

Solution

The most portable approach is to make use of Scheme's generic formatting logic to generate a string based on conditionals:

(define (monster-string n)
  (let ((text (if (= n 1) (list "is" 1 "")
              (list "are" n "s"))))
    (printf "There ~a ~a monster~a approaching"
            (car text) (cadr text) (caddr text))))

> (monster-string 1)
There is 1 monster approaching
> (monster-string 15)
There are 15 monsters approaching
> 

An alternative approach would be to define a set of functions to present the correct syntax:

(define (is-are n)
  (if (= n 1) "is" "are"))

(define (plural n)
  (if (= n 1) "" "s"))

(define (monster-string2 n)
  (printf "There ~a ~a monster~a approaching"
          (is-are n)
          n
          (plural n)))

> (monster-string2 1)
There is 1 monster approaching
> (monster-string2 15)
There are 15 monsters approaching
> 

Discussion

Not much else to say. Perl's Lingua::EN support is far superior to Scheme here. Good idea for a library we could write!

See Also

Comments go here

CookbookForm
TopicType: Recipe
ParentTopic: NumberRecipes
TopicOrder: 150

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest