# String to function and vice versa

**URL:** <https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 23, 2019, 6:58am UTC](https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606 "2019-12-23T06:58:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [December 23, 2019, 6:58am UTC](https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606/1 "2019-12-23T06:58:27Z")

</div>

How can a string like “x^3 + 6_x^2 + 6_x” be turned into a function

```
p(x) = x^3 + 6*x^2 + 6*x

```

and conversely, given the function p, how to extract the body of the function as a string?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 23, 2019, 7:46am UTC](https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606/2 "2019-12-23T07:46:08Z")

</div>

```julia
julia> eval(Meta.parse("p(x) = x^3 + 6*x^2 + 6*x"))
p (generic function with 1 method)

julia> p(2)
44

julia> p(3)
99

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 23, 2019, 2:24pm UTC](https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606/3 "2019-12-23T14:24:01Z")

</div>

Make sure you are not passing functions as strings. If you want a caller to pass a function like `x^3 + 6x^2 + 6x`, have them pass an anonymous functions like `x -> x^3 + 6x^2 + 6x`, not a string that you need to parse and `eval`.

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [December 23, 2019, 11:46pm UTC](https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606/4 "2019-12-23T23:46:48Z")

</div>

The situation is the reverse: I am the caller, and I get a structure returned in which the polynomials are given in this form. I have to convert them to functions. So I do not see a better solution then jling’s.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 23, 2019, 11:59pm UTC](https://discourse.julialang.org/t/string-to-function-and-vice-versa/32606/5 "2019-12-23T23:59:35Z")

</div>

Why are the polynomials in that form? Can’t they just be returned as an array of coefficients for example?
