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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:10:31+00:00 2026-06-13T19:10:31+00:00

I am pretty new to programming and I was asked to convert 3 haskell

  • 0

I am pretty new to programming and I was asked to convert 3 haskell functions into python as a practice exercise.
The 3 functions are connected, since output of one is used as the input of the next one and so on.

I get what the haskell functions do, but I have no idea how to start converting them!

This is the haskell code:

factorial :: Int -> Int
factorial n = product (down n)

product :: [Int] -> Int
product [] = 1
product (a:as) = a * product as

down :: Int -> [Int]
down 0 = []
down n = n : down (n-1)

And this was my try of converting it:

class function:
    def down(self):
        if self.n == 0:
            self.lista = []
        else:
            self.lista = range(self.n, 0, -1)

    def product(self):
        for x in self.lista:
            if x == []:
                self.product = 1
            if x != []:
                for i in self.lista:
                    self.product = i * self.product

    def factorial(self):
        self.n = int(raw_input("n="))

        self.fact = self.product(self.down(self.n))

        print self.fact
        raw_input()

c = function()
c.factorial()

Well, first I believe this isn’t a ‘direct conversion’ of the haskell code. That would be okay though, but second, it doesn’t work.

This lack of programming background is killing me…can anyone help me with this problem?

Thanks a lot!

Edit:

The point of this issue is to exactly convert the haskell into python.
I did a stripped version of it myself, it was the next step in the exercise ^^

  • 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-13T19:10:32+00:00Added an answer on June 13, 2026 at 7:10 pm

    First off, ditch the class wrapper – that’s not needed.

    A straight Python translation would be something like:

    # factorial :: Int -> Int
    def factorial(n):
        return product(down(n))
    
    # product :: [Int] -> Int
    def product(arr):
        if len(arr) == 0: return 1
        a, ar = arr[0], arr[1:]
        return a * product(ar)
    
    # down :: Int -> [Int]
    def down(n):
        if n == 0: return []
        return [n] + down(n - 1)
    

    But the recursive style is not very Pythonic here. Next exercise: replace recursion with iteration, list comprehensions, or calls to reduce (if on Python2).

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

Sidebar

Related Questions

I'm pretty new to Python programming and would appreciate some help to a problem
I'm pretty new to programming and am having some trouble understanding the output of
I'm pretty new to programming and I'm creating a python game for my little
I'm pretty new to Python programming so I have this question: How can I
I'm pretty new to programming for Linux environments, so I don't exactly know what
I'm pretty new to programming, and I'm creating a simple text-based game.> I'm wondering
I'm pretty new to programming C# and windows phone... In my Windows Phone App
I’m pretty new to programming, so bear with me if my question isn’t specific
Hi I am still pretty new to programming and I haven't had the chance
I'm pretty new in iphone programming and have stumbled upon this issue which I

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.