Arc Forumnew | comments | leaders | submitlogin
Problem with factorial
2 points by jsgrahamus 2421 days ago | 19 comments
C:\Users\Steve\Desktop\arc3\arc3>mzscheme -m -f as.scm

Use (quit) to quit, (tl) to return here after an interrupt.

arc> (def factorial (n)

    (if (> n 0)

        (* n (factorial (- n 1)))

        1))
#<procedure: factorial>

arc> (factorial 50)

Error: "reference to undefined identifier: _M"

arc>



4 points by rocketnia 2421 days ago | link

That "reference to undefined identifier: _M" error happens for me all the time. If I copy from a CRLF source into a console window on Windows XP, and then I complete the entry by just pressing enter, then all the versions of MzScheme/Racket I've used get confused about what counts as a newline. They apparently treat carriage returns as a capital M (in MzScheme) or a capital R (in Racket v5.1.1).

Scary that the letter corresponds with the name of the application. ^^; Actually, by the time the Racket rename happened, I was already in the habit of working around this by including newlines at the end of what I was pasting, so I never noticed Racket treated it as an R until just now....

  Welcome to Racket v5.1.1.
  >
  (define (foo R) (+
    1 2 3))
  > (foo 1000)
  1006
  >
The string I pasted there was:

  "\r\n(define (foo R) (+\r\n  1 2 3))"
Then I pressed enter. So more accurately, it treats \r as some amount of whitespace followed by a capital R.

  >
  "
  "
  
  )
  "
  "\r\nR\r\n\r\n)\r\n"
  >
The string I pasted there was:

  "\r\n\"\r\n\""
And then I followed it by typing [enter][enter][)][enter]["][enter], trying to get Racket to recognize my input as a full command.

So okay, more accurately: It treats matches of the regex /\r\n./ as though they're "\r\nR".

-----

1 point by akkartik 2421 days ago | link

Ah, so the difference in the two examples is just that one was pasted! That makes sense, thanks a lot.

When I open a dos file in vim the extra CR is sometimes rendered as ^M. I wonder if the _M has something to do with that.

-----

1 point by rocketnia 2421 days ago | link

I think that was my original theory. I probably even saw the R at some point, thought it came about because of the \r escape sequence, and forgot it wasn't like that all along.

Keep in mind that the underscore is added by the Arc compiler though. The M or R is being successfully compiled as a global variable reference. ^_^

  Use (quit) to quit, (tl) to return here after an interrupt.
  arc>
  '(
  Pocket-N/A)
  (Rocket-N/A)
  arc>

-----

2 points by jsgrahamus 2421 days ago | link

In my case, I typed everything into Arc. And I still got that error.

-----

1 point by rocketnia 2421 days ago | link

Nice. Since we're on different versions of Windows, I can totally believe that. But you only got it for one definition of factorial and not the other? Did you type them in differently somehow? (Say, by putting a newline at the beginning of the second one, or by entering the first one as the very first command of the REPL session?)

-----

[deleted]
1 point by rocketnia 2420 days ago | link

Actually, there's a chance I may be able to even though I'm on XP, since it may have more to do with the version of MzScheme being used. I'll see what I can do.

EDIT: Nope, no luck.

-----

1 point by jsgrahamus 2420 days ago | link

Thanks, everyone, for the help. It works great now:

C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1>"c:\Program Files (x86)"\racket\racket -f as.scm

Use (quit) to quit, (tl) to return here after an interrupt.

arc> (def factorial (n) (if (< n 2) 1 (* n (factorial (- n 1)))))

#<procedure: factorial>

arc> (factorial 50)

30414093201713378043612608166064768844377641568960512000000000000

arc>

-----

1 point by jsgrahamus 2421 days ago | link

Odd that this did work, when the other did not.

(def factorial (n)

   (if (< n 2) 

      1 

      (* n (factorial (- n 1)))))

-----

2 points by akkartik 2421 days ago | link

Very odd indeed. I gather you're on windows. What version of arc and mzscheme do you have?

On my setup both examples work.

-----

1 point by zck 2421 days ago | link

Both work for me too. I'm using Ubuntu 11.04, Arc 3.1, and I tested both with Racket 5.1.1 and mzscheme 4.2.1 .

jsgrahamus, can you see if it works for a lower call to factorial? Does it work at all? Even (factorial 0) ?

-----

1 point by jsgrahamus 2420 days ago | link

Having loaded Racket 5.1.1, Arc 3.1 and made a change in ac.scm, everything works, even (factorial 0).

-----

1 point by jsgrahamus 2421 days ago | link

I'm on Windows 7x64, mzscheme 372 and I just downloaded arc this morning.

-----

1 point by rocketnia 2421 days ago | link

This is a bit off-topic, but despite your best efforts, you are probably not using the latest version of Arc. Sounds like you've been following the instructions at http://www.arclanguage.org/install for the whole time you've been posting here, and those are out of date by almost two years, even linking to a version of Arc 3 (one of a few Arc 3s, I think :-p ) instead of Arc 3.1. I think this is a better starting resource: http://sites.google.com/site/arclanguagewiki/.

Old versions of Arc needed to run on MzScheme 372 because it was the last MzScheme with mutable lists, but now Arc 3.1 uses some pointer manipulation to mutate lists behind Racket's back, meaning any version of Racket is fine. However, this actually introduced a rare garbage collection bug, if you can believe it. ;) That means there is actually a legitimate reason to keep using MzScheme 372, but as described at http://sites.google.com/site/arclanguagewiki/arc-3_1/known-b..., pretty much every Arc setup besides the official one avoids this bug, and it's possible to patch the official one too.

Anyway, you're the first person I've heard of who's tried Arc on Windows 7 (which isn't to say there aren't Arc Forum lurkers in the same position ^_^ ). If you encounter Arc-on-Windows-7 stumbling blocks, please continue talking about them like you have in this thread!

-----

1 point by jsgrahamus 2420 days ago | link

Just downloaded arc3.1 and am trying to run it against Racket 5.1.

C:\Users\Steve\Desktop\arc3\arc3.1>c:"\Program Files (x86)"\Racket\Racket -m -f as.scm main: not defined or required into the top-level environment

C:\Users\Steve\Desktop\arc3\arc3.1>c:"\Program Files (x86)"\Racket\mzscheme -m - f as.scm main: not defined or required into the top-level environment

C:\Users\Steve\Desktop\arc3\arc3.1>

-----

1 point by rocketnia 2420 days ago | link

The -m option means to call the "main" function now. Just leave it out. It used to mean "--mute-banner", which suppressed the "Welcome to MzScheme" line.

-----

1 point by jsgrahamus 2420 days ago | link

C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1>"c:\Program Files (x86)\Racket"\racket

Welcome to Racket v5.1.1.

> user break

=== context ===

c:\Program Files (x86)\Racket\collects\racket\private\misc.rkt:85:7

>

C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1>"c:\Program Files (x86)\Racket"\racket -d as.scm

default-load-handler: expected a `module' declaration for `as', found: something else in: #<path:C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1\as.scm>

=== context ===

standard-module-name-resolver

C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1>"c:\Program Files (x86)\Racket"\racket -f as.scm

ffi-obj: couldn't get "setuid" from #f (The specified

procedure could not be found.; errno=127)

=== context ===

c:\Program Files (x86)\Racket\collects\ffi\unsafe.rkt:176:2: get-ffi-obj*

C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1\ac.scm: [running body]

C:\Users\Steve\Documents\Programming\Lisp\arc\arc3.1>

-----

2 points by rocketnia 2420 days ago | link

That "racket -f as.scm" command is the right one. The 'setuid issue is one of a few known issues on Windows, and it has a known fix: http://arclanguage.org/item?id=10625.

I give a whole step-by-step process for setting up Arc on Windows XP at http://www.arclanguage.org/item?id=12397. This link actually appears on http://sites.google.com/site/arclanguagewiki/, but it's okay if you didn't read that far down. :-p

By the way, I committed a 'setuid fix to Anarki a while ago, and unlike the fix above, this version of 'setuid should still work as usual on Linux: https://github.com/nex3/arc/commit/3201b34f3ed89e6305b0d9906...

-----

1 point by jsgrahamus 2420 days ago | link

Thanks for the tip and the understanding ;-)

Works great.

-----

1 point by rocketnia 2420 days ago | link

Erm, make sure to take another look. I've been ninja-editing that post for the last ten minutes. XD

-----