Arc Forumnew | comments | leaders | submitlogin
String to code
3 points by bOR_ 6014 days ago | 3 comments
An (also lisp inexperienced) friend of mine ran into a problem, and I can't seem to solve it either (and I'm bound to need it later on).

How, in arc or scheme would you change a string into code?

  (??? "(+ 5 5)")

  >10


8 points by tokipin 6014 days ago | link

read: http://arcfn.com/doc/io.html#read

  (eval (read "(+ 5 5)"))

  (eval:read "(+ 5 5)") ; <- maybe nicer
there might be a better way. eval applies everything in the global scope, which is ok in some instances and incovenient in others

-----

2 points by bOR_ 6013 days ago | link

Thanks. from this I could figure out the mzscheme version to help my friend out, and for me it hopefully offers me a way for my monkeys to copy behaviour from each other in a way that is still editable (and not just #procedure)

  boris:falcon:~/arc2:rlwrap mzscheme '(+ 5 5)'
  Welcome to MzScheme version 352, Copyright (c) 2004-2006 PLT Scheme Inc.
  > argv
  #1("(+ 5 5)")
  > (vector-ref argv 0)
  "(+ 5 5)"
  > (require (lib "string.ss"))
  > (eval-string i)
  10
  >

-----

3 points by absz 6013 days ago | link

It's probably a better idea to have your monkeys copy code trees around; i.e., rather than having (= monkey1!code "(+ x y)"), have (= monkey1!code (read "(+ x y)")), which is the same as (= monkey1!code '(+ x y)). A scheme like this (if you are careful about what you do with symbols) is a better idea because it's more Lisplike and you can thus take advantage of Lisp's strengths (list manipulation and code-is-data).

-----