Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:21:33+00:00 2026-06-08T19:21:33+00:00

In reading R for programmers I saw this function oddcount <- function(x) { k

  • 0

In reading R for programmers I saw this function

oddcount <- function(x) {
  k <- 0
  for (n in x) {
    if (n %% 2 == 1) k <- k+1
  }
  return(k)
}

I would prefer to write it in a simpler style (i.e in lisp)

(defn odd-count [xs]
  (count (filter odd? xs)))

I see the function length is equivalent to count and I can write odd? so are there built-in map/filter/remove type functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:21:35+00:00Added an answer on June 8, 2026 at 7:21 pm

    A more R way to doing this would be to avoid the for loop, and use vectorization:

    oddcount <- function(x) {
      sum(x %% 2)
    }
    

    The comparison between x and 2 outputs a vector as x itself is a vector. Sum than calculates the sum of the vector, where TRUE equals 1 and FALSE equals zero. In this way the function calculates the number of odd numbers in the vector.

    This already leads to more simple syntax, although for non-vectorization-oriented people the for loop tends to be easier to read. I greatly prefer the vectorized syntax as it is much shorter. I would prefer to use a more descriptive name for x though, e.g. number_vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi clever programmers, I've been searching and reading a couple days, but I need
I'm a C++ programmer, and I was reading this site when I came across
Reading the Scala by Example book and there is this example when Martin explains
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I'm just after reading the User Interface Design for Programmers 2001 by Joel Spolsky.
Possible Duplicate: What C++ idioms should C++ programmers use? After reading books like C++
I was reading this Sun's tutorial on Thread . I found a block of
I'm reading Understanding Linux Kernel. This is the snippet that explains how Linux uses
I was reading the Python docs about classes and came across this paragraph which
i ve post this question in programmers.stackexchange.com also and they told me that it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.