Newsgroups: comp.sys.ibm.pc.misc Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!cornell!uw-beaver!mit-eddie!mintaka!bloom-picayune.mit.edu!athena.mit.edu!pshuang From: pshuang@athena.mit.edu (Ping-Shun Huang) Subject: Re: Programming Question In-Reply-To: sheaf@helios's message of 20 Jun 91 16:47:15 GMT Message-ID: Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology References: <142295@unix.cis.pitt.edu> Distribution: usa Date: Fri, 21 Jun 91 21:14:04 GMT Lines: 35 In article <142295@unix.cis.pitt.edu> sheaf@helios (Sheaf) writes: > So, to put it simply, is there any way (in any language) to define a > function by user input? Hmm... you're using Pascal and BASIC, right? I don't suppose switching to a language like Scheme or Lisp is a viable option, right? If you're using interpreted BASIC, it should be possible for you to create a text file on disk with the user's input, like this: 30000 DEF FN(X)=X^2+3*X^3 ; can't remember correct syntax Depending on which BASIC you are using, it may be possible for you to MERGE that line into your program. You could then use the defined numeric function as you please. In Pascal this would be much more difficult, unless you're running interpreted Pascal. What you really want, and which is available in various, more exotic languages (i.e. C and assembler and BASIC and Pascal don't have it directly {grin}) is an eval function, which would evaluate any expression which is legal in the language itself. (I could tell some stories about one of the projects at the end of the term for the first computer science class at MIT... writing a Scheme interpreter in Scheme itself, i.e. creating a meta-circular evaluator.) In your case, you would only need an eval function capable of parsing arithmetic and algebraic expressions, which is still not trivial unless you're willing to ask your users to adopt postfix or prefix notation rather than infix. Essentially you would have to write a recursive routine which would take user input as a string, break it up into the constituent tokens, and then interpret the tokens on the fly everytime your graphing application needed the function. -- Above text where applicable is (c) Copyleft 1991, all rights deserved by: UNIX:/etc/ping instantiated (Ping Huang) [INTERNET: pshuang@athena.mit.edu]