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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:18+00:00 2026-06-17T12:17:18+00:00

The following implementation will loop forever. pick_nums(0,_,_). pick_nums(Count,From,To) :- random_member(X,From), ( member(X,To) -> pick_nums(Count,From,To)

  • 0

The following implementation will loop forever.

pick_nums(0,_,_).
pick_nums(Count,From,To) :-
   random_member(X,From),
   ( member(X,To) -> pick_nums(Count,From,To)
   ;  C1 is Count-1,
      pick_nums(C1,From,[X|To]) ) .

?- numlist(1,9,X), pick_nums(3,X,Y).
  • 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-17T12:17:18+00:00Added an answer on June 17, 2026 at 12:17 pm

    The problem here is that inside pick_nums(Count, From, To) you have an identical call of pick_nums(Count, From, To). The conditional logic here seems to be saying “get me a random element of From, but try again if I already have it in To; otherwise, recur normally.” The basic problem here is simply that your condition always succeeds.

    I’m sure there’s a more direct solution to your problem than rewriting from scratch, but it seemed like somewhat unidiomatic Prolog to me. Instead, let’s rethink what we’re trying to say in case we can say it “logically” more directly. What you’re actually trying to say is “get me a random subset of From of with a certain size.” The conditional in your original code is really just a foil for “hey, I’ve already seen this element so let’s move on” but frequently in Prolog you get further by saying what you mean than telling Prolog how to do it.

    pick_nums(0, _, []).
    pick_nums(Count, From, [X|SelectedFromRemaining]) :-
      random_member(X, From),
      select(X, From, Remaining),
      C1 is Count - 1,
      pick_nums(C1, Remaining, SelectedFromRemaining).
    

    By using select/3 I’m able to produce the list without the selected element in it, which I can then pass on to the next call. This way I don’t have to worry about whether or not I’ve seen this element already. Note that there could be a performance penalty associated with doing it this way, but we got there in fewer statements and it (hopefully) is more clear what we’re trying to do.

    If we’re willing to depend further on SWI-Prolog there are some built-in libraries that can simplify this process even further. For instance, the random library has a predicate that will give us a random permutation of a list. We can state what we’re doing pretty clearly using that:

    pick_nums(Count, From, To) :-
      random_permutation(From, Scrambled),
      append(To, _, Scrambled),
      length(To, Count).
    

    If you always expect to use it with numlist there’s another helper in there that does this already:

    ?- randset(3, 9, X).
    X = [3, 4, 8].
    
    ?- randset(3, 9, X).
    X = [2, 7, 9].
    

    I hope this is enough help.

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

Sidebar

Related Questions

Take the following naive implementation of a nested async loop using the ThreadPool: ThreadPool.SetMaxThreads(10,
have the following implementation $.ajax({ type: POST, url: /Member/SaveMember, data: $('form').serialize(), success: refreshGrid() how
In the following implementation of a hypothetical navigation module the module object returns properties
I recently saw the following implementation of enqueue for a BlockingQueue ( source )
I have a problem with the following implementation of hook_cron in Drupal 6.1.3. The
Given I have two File objects I can think of the following implementation: public
The following XML implementation seems to work fine: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent
I have this following codes: @implementation MyImageView @synthesize image; //image is a UIImage -
How can the following simple implementation of sum be faster? private long sum( int
I have the following code implementation of my generic singleton provider: public sealed class

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.