Блог пользователя wjomlex

Автор wjomlex, 13 лет назад, По-английски

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

  • Проголосовать: нравится
  • +37
  • Проголосовать: не нравится

»
13 лет назад, # |
Rev. 3   Проголосовать: нравится +5 Проголосовать: не нравится

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

  • »
    »
    13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

  • »
    »
    13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.