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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:23:45+00:00 2026-06-14T22:23:45+00:00

I’m trying to define some basic Primitive Recursive functions in Haskell. Why is my

  • 0

I’m trying to define some basic Primitive Recursive functions in Haskell. Why is my times function recursing one too many times (ie eval times[x,y] is resulting in (x+1)*y)? I think my problem is generally due to a poor understanding of how the Composition function works. Please don’t give an answer without an explanation to clarify my understanding.

 import Prelude hiding (pred,and,or,not)

 data PR = Z
         | S
         | P Int
         | C PR [PR]
         | PR PR PR
         deriving Show
 eval :: PR -> [Integer] - Integer
 eval Z _ = 0
 eval S [x] = x+1
 eval (P n) xs = nth n xs
 eval (C f gs) xs = eval f (map (\g -> eval g xs) gs)
 eval (PR g h) (0:xs) = eval g xs
 eval (PR g h) (x:xs) = eval h ((x-1) : eval (PR g h) ((x-1):xs) : xs)

 nth _ [] = error "nth nil"
 nth 0 _ = error "nth index"
 nth 1 (x:_) = x
 nth (n) (_:xs) = nth (n-1) xs

 one = C S [Z]
 plus = PR (P 1) (C S [P 2])
 times = PR (P 1) (C plus [P 2, P 3])

I’ve tried a few other things for times the closest being times = PR (P 1) (C plus[P 2, P 2] but this comes out to 2x*y I thought “Well I’ll just replace one of those P 2‘s with Z and then it will be x*y” This actually makes it the identity function of y and I have no idea why.

  • 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-14T22:23:46+00:00Added an answer on June 14, 2026 at 10:23 pm

    This definition for times seems to work:

    times' = PR Z (C plus [P 2, P 3])
    
    *Main> eval times' [6,7]
    42
    

    This makes sense since 0*x = 0 not 1.

    Note that I had to change the definition of eval (C ...) in order for it to compile:

    eval (C f gs) cs = eval f (map (\g -> eval g cs) gs)
    

    More detailed explanation…

    We know that times will be of the form PR Z h for some h.

    Let’s expand eval (PR Z h) (x+1:y:ys) …

    eval (PR z h) (x+1:y:ys)
        = eval h ((x+1-1) : eval (PR g h) ((x+1-1):y:ys) : y : ys)
        = eval h (x : eval (PR Z h) (x:y:ys) : y : ys)
        = eval h (x : x*y : y : ys)
    

    because by induction we know eval (PR z h) (x:y:ys) = x*y.

    So what does h have to be in order to get (x+1)*y = y+x*y? We need to add y (which is P 3) and x*y (which is P 2), so we should define h as:

    h = C plus [P 2, P 3]
    

    If you use P 1 instead of Z, then your base case is y and not 0:

    eval (PR (P 1) ...) (0:y) = eval (P 1) (y) = y
    

    The recursion stays the same, so you’re off by y in your answer.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.