Thread: How to represent functional expressions

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    How to represent functional expressions

    Hi, I have the following problem. I want to make some data structure to represent all possible expressions of a certain form. They are defined as follows:

    0 is a term.
    if a_1, a_2, ... a_k are terms, then a_1 + a_2 + ... a_k is a term.
    if a_1, a_2, ... a_k are terms, then f(a_1, a_2, ..., a_k) is a term.

    So the terms are all expressions I can build up from 0 using + and f. How do I define a data structure to store terms? The simpler the better.

    Thanks for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The silly answer is "a string".

    If you actually want something where only valid expressions can be stored, then you would probably want an expression tree.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First off we are not a "free code" service.

    Most here will help you within their various expertise as best they can but the prerequisite is that you make some effort and produce some code of your own, asking for help when you get stuck. We will do this with you but we're not going to do it for you.

    Next... if you haven't already, get hold of a good book on C programming. Pour through it page by page, exercise by exercise, repeating as necessary until you understand what's going on... We will help you learn, but we're not hear to teach C either.

    So... do the work... if you get stuck --really stuck, not lazy stuck-- post your code and lets see what we can do...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    The silly answer is "a string".

    If you actually want something where only valid expressions can be stored, then you would probably want an expression tree.
    I would interpret that as an array... then the sum of it's elements.

    If it's not that, then he's going to be awfully disappointed when he finds out that C is a programming language, not a calculator.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Quote Originally Posted by CommonTater View Post
    First off we are not a "free code" service.

    Most here will help you within their various expertise as best they can but the prerequisite is that you make some effort and produce some code of your own, asking for help when you get stuck. We will do this with you but we're not going to do it for you.

    Next... if you haven't already, get hold of a good book on C programming. Pour through it page by page, exercise by exercise, repeating as necessary until you understand what's going on... We will help you learn, but we're not hear to teach C either.

    So... do the work... if you get stuck --really stuck, not lazy stuck-- post your code and lets see what we can do...
    I am not asking for free code. I asked for a description of a data structure that would be appropriate for my data. Any useful ideas would be welcome.

    You ask me to post code before asking the question, but isn't that silly in this case? I don't even know what data structure I am going to use, how can I post code?

    I am not "lazy stuck", I have thought about this for a while before coming here and haven't come up with a solution.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Deedlit11
    I asked for a description of a data structure that would be appropriate for my data.
    What are you going to do with the data?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Deedlit11 View Post
    I am not asking for free code. I asked for a description of a data structure that would be appropriate for my data. Any useful ideas would be welcome.

    You ask me to post code before asking the question, but isn't that silly in this case? I don't even know what data structure I am going to use, how can I post code?
    Lase is correct in asking... What data? What are you doing with it? What are you hoping to accomplish?

    We can't help you if we don't know these things...

    Finding the solution to a problem ALWAYS starts with understanding the problem itself.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Quote Originally Posted by laserlight View Post
    What are you going to do with the data?
    It's for mathematical research. Each term stands for an ordinal, and the function f stands for the extended Veblen function. For my research I need to be able to manipulate these ordinals, and do stuff to them like add, multiply, exponentiate, and take fundamental sequences. For example, the nth term in the fundamental sequence of f(X + f(0)) is f(X) + f(X) ... + f(X) with n f(X)'s, where X is some term. So in this case I would need to delete the f(0), replicate the structure n times, then combine them with +'s. So the operations get pretty complex.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Deedlit11 View Post
    It's for mathematical research. Each term stands for an ordinal, and the function f stands for the extended Veblen function. For my research I need to be able to manipulate these ordinals, and do stuff to them like add, multiply, exponentiate, and take fundamental sequences. For example, the nth term in the fundamental sequence of f(X + f(0)) is f(X) + f(X) ... + f(X) with n f(X)'s, where X is some term. So in this case I would need to delete the f(0), replicate the structure n times, then combine them with +'s. So the operations get pretty complex.
    Well, that didn't help a bit....

    Do you know any C programming at all? (As in have you taken courses, read books or studied tutorials?)

    I'm betting not... so that's what you need to do first. You need to learn C... Just grab any book or extended tutorial you can find, work it page by page, example by example, repeat as needed until you understand what is going on... Then revisit your calculations with an eye to how C solves problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Represent Maze
    By iamnew in forum C++ Programming
    Replies: 37
    Last Post: 04-28-2010, 08:47 PM
  2. functional derivation better?
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2007, 12:57 AM
  3. Functional programming languages... r they really functional?
    By code_mutant in forum C++ Programming
    Replies: 10
    Last Post: 02-25-2004, 05:29 AM
  4. functional dependencies
    By DMaxJ in forum C++ Programming
    Replies: 10
    Last Post: 10-23-2002, 07:07 AM
  5. Problem with non-functional window
    By Magos in forum Windows Programming
    Replies: 5
    Last Post: 04-14-2002, 11:32 PM