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

  • Home
  • SEARCH
  • 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 8781125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:13:14+00:00 2026-06-13T20:13:14+00:00

I am attempting to memoize my implementation of a Pascal’s triangle generator, as a

  • 0

I am attempting to memoize my implementation of a Pascal’s triangle generator, as a Ruby learning experiment. I have the following working code:

module PascalMemo
  @cache = {}
  def PascalMemo::get(r,c)
    if @cache[[r,c]].nil? then 
      if c == 0 || c == r then 
        @cache[[r,c]] = 1
      else
        @cache[[r,c]] = PascalMemo::get(r - 1, c) + PascalMemo::get(r - 1, c - 1)
      end
    end
    @cache[[r,c]]
  end
end

def pascal_memo (r,c)
  PascalMemo::get(r,c)
end

Can this be made more concise? Specifically, can I create a globally-scoped function with a local closure more simply than this?

  • 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-13T20:13:15+00:00Added an answer on June 13, 2026 at 8:13 pm
    def pascal_memo
      cache = [[1]]
      get = lambda { |r, c|
        ( cache[r] or cache[r] = [1] + [nil] * (r - 1) + [1] )[c] or
          cache[r][c] = get.(r - 1, c) + get.(r - 1, c - 1)
      }
    end
    
    p = pascal_memo
    p.( 10, 7 ) #=> 120
    

    Please note that the above construct does achieve memoization, it is not just a simple recursive method.

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

Sidebar

Related Questions

Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
Attempting/struggling to get registration and sign-up working within an active admin project. I have
Attempting to compile the following code under Qt SDK 4.7.4 for Desktop - MinGW
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
Attempting to learn Ruby using the new LearnStreet online tutorials. Have attempted to get
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
Attempting to use a custom hex color for my css triangle (border). However since
Attempting to run runsnake gives ImportError: No module named wx Opening an ipython or
Attempting to database driven website for video rentals So I have two tables in
Attempting to install ruby 1.9.3 on OSX Lion 10.7.3. During the install I received

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.