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 6139717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:01:36+00:00 2026-05-23T18:01:36+00:00

I am trying to write a Tail Recursive procedure to count the number of

  • 0

I am trying to write a Tail Recursive procedure to count the number of uninstantiated variables in a list. I am a little stuck, where am I going wrong.

My current query is below:

count([S,L],N) :- var(S), !, N+1.
count([L],N).
  • 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-05-23T18:01:37+00:00Added an answer on May 23, 2026 at 6:01 pm

    Note: this answer presents a solution that is recursive but not tail recursive. For a tail recursive solution you should use an accumulator as can be shown in other answers from this question.

    As with any recursive procedure, you should add a proper base case.
    In this case, it should be a clause with an empty list that returns unifies 0 with the number of uninstantiated variables:

    count([], 0).
    

    Check the clause you’ve written. It takes as input a list of two elements instead of a list represented as a Head item and a tail list, and it really does nothing with N:

    count([Head|Tail], M):- 
        var(Head), 
        !, 
        count(Tail, N), 
        M is N+1.
    

    And finally, you should also add a clause to deal with the case when the first item of the list is not an uninstantiated variable:

    count([_|Tail], N):- count(Tail, N).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a procedure order(List,Result) that has a List as input
im trying to write an app that will display a list off lines from
I'm trying to write hasRepetition that 'recives' a list and returns true if and
I wanted to have a tail-recursive version of List.map , so I wrote my
Trying to write a trivial application, But I have stuck into one of the
I'm trying to write a method that merges a doubly linked list in an
Working in Haskell, I am trying to write a big list of booleans to
I was trying to write recursion function,to find factorial of a number. int factorial(int
I'm trying write a query to find records which don't have a matching record
Trying to write a PowerShell cmdlet that will mute the sound at start, unless

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.