wjomlex's blog

By wjomlex, 12 years ago, In English

I did the first problem pretty quickly after learning how to duplicate the top element of the stack (it took me way too long to figure out what "dup" meant).

And then I spent 30 minutes trying to figure out how you do loops only to realize that of course there aren't any loops, 'cause it's a functional language XD

(As kilotaras has pointed out, there are indeed general loops in Factor. There's even a keyword called "loop" :P)

Besides problem A, most of my code follows the same paradigm: a recursive function that keeps track of the input we haven't used, and the answer we've calculated on the input we have used.

After a couple problems I learned about "::" for binding function parameters to local variables. Boy, does that save time.

And now that the contest is over I see that there's a "prime" function. That would surely speed up my Problem I enough because right now it's just horrendously naive trial division.

Editorial

A quick mini-editorial of how your recursive state might look for each problem:

  • A: No recursion necessary!
  • B: (output string, left-most portion of number)
  • C: (output string, current divisor, unfactored portion of number)
  • D: (processed portion of string, unprocessed portion)
  • E: (boolean flag that keeps track of if you've seen "H", "Q", or "9", unprocessed portion of string)
  • F: (number of zeros so far, left-most 10 or so non-zero digits, factorial remaining to multiply by)
  • G: I haven't done this, but it's a more complicated version of B. I think it's easily the hardest problem.
  • H: (processed portion of string, unprocessed portion)
  • I: (boolean flag that keeps track of if all suffixes have been prime so far, left-most unprocessed digits) I also reuse my function from C to factor each number
  • J: (A stack representing unmatched open parentheses, unprocessed portion of string)

Code

  • Vote: I like it
  • +37
  • Vote: I do not like it

»
12 years ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

Factor had a very nice search over documentation, resulting in solutions like this.
P.S. Factor has loops.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Ha! Very nice :D

    I saw that Factor had loops over sequences, and a notion of numbered loops, but I was thinking of a really general "while" or so.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Oh, there's a "loop" keyword? How did I miss that? XD

    You're right, Factor has a really nice docs search. It's sort of important given that, otherwise, I found it really hard to figure out the structure of the documentation.

    Now I feel like reworking all of these solutions :P

    Although it was really nice to flex the functional programming skillz again. Haven't had any cause to do functional programming since my undergrad.