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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:26:44+00:00 2026-05-13T21:26:44+00:00

Okay, so I have another question on a prolog homework problem I am struggling

  • 0

Okay, so I have another question on a prolog homework problem I am struggling with. The problem goes as follows:

Write a Prolog program that will take a list representing the bits of a binary number, and return the decimal value of the integer that the list represents.

Example: valueof([1,1,0,1],X).
X = 13

So here’s what I have so far:

valueOf(List, X) :- setup(List, [], X).
setup([H|T], [Y|H], X) :- setup(T,Y,X).
setup([], Y, X) :- count(Y, X, 0).
count([], _, _).
count([H|T], X, Count) :- NewCount is Count + 1, NewX is X + (H*2^Count),
                          count(T,NewX,NewCount).

Once again, I appreciate any help at all, I really seem to be struggling with this prolog stuff. Thanks.

  • 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-13T21:26:44+00:00Added an answer on May 13, 2026 at 9:26 pm

    I would say that I disagree with Carl’s choice of base case. We must handle the case of empty list, and it is better not to have multiple types of “base cases.” The analogy of “base” case is the case at the “bottom” of the recursion and we all know things have only one bottom, or base.

    So I would suggest:

    • base case:
      • empty list, which has the value of “zero” (0)
      • this case concludes the recursion and so must copy any accumulated value into the output parameter.
    • initial case: having not yet accumulated any digits, the accumulator variable will be zero (0).
    • recursive case: the digit examined in each recursion is accumulated into the accumulator

    The prolog code will look like this:

    % Initial value of accumulator is zero (0).
    valueOf( L, X) :- value_of_accumulation( L, 0, X).
    
    % Base case:
    % When there are no more digits to process, the output value (3rd param)
    % is unified with the accumulator (2nd param)
    value_of_accumulation( [], A, A).
    
    % The recursive step.  There is a digit (B) to process here.  T is a temporary
    % which holds the value of the accumulator "passed down" to the next case
    % and, ultimately, to the base case after possible more accumulations.  A holds
    % the previously-accumulated value derived from more-significant bits, already
    % processed in previous recursions.  This clause is invoked once for each bit.
    % Implicitly, the invocations of this clause generate a "count" but there is no
    % actual use of the count when accumulating the bits.  Each bit is accumulated
    % without knowledge of whether subsequent bits follow.
    value_of_accumulation( [B | L], A, X) :-
        B >= 0,
        B =< 1,
        T is % fill this in with math, a function of bit B and previously-accumulated bits A,
        value_of_accumulation( L, %fill this in with prolog understanding.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, another SQL question. What I am trying to achieve is to show by
Okay, got a question. I have assembled a bit mask for options. Basically my
I have a quick question regarding the use of typedefs for lengthy templates. The
I have a large form that consists of all the input (text, checkbox, radio,
Consider a Wicket WebPage that redirects to another page (based on some logic omitted
I have approached you today to try and gather your opinions and experience with
I have a div container, and I call it "content_container". This container is able
I'm trying to set up a program to record a portion of an internet
I use the backend solution from django. I just want to get a username
I am writing a rather large JavaScript based application and sometimes there are cases

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.