0

Here,I have a diff function which can be used to differentiate.

for example diff (sin x) ("x")= cos x

(here,(sin x) is the function, ("x") is the variable i want to differentiate)

How to use iterate to generate a list of differentiations of function [f(x),f'(x),f'(x),f''(x)....]

overpro
  • 49
  • 8
  • use "iterate"command – overpro Jan 04 '15 at 20:02
  • 1
    For future questions, please quote proper declarations of the functions you're talking about (type signatures!) and use Markdown to make your post nicely readable. – leftaroundabout Jan 04 '15 at 20:57
  • 1
    An OT suggestion: when programming, especially in a functional programming language, please try to unlearn the bad habit of saying "`sin x` is a function" when actually meaning "`\x -> sin x` is a function". In your case, I guess, `sin x` is actually some value representing an _expression_, i.e. a piece of syntax, using a suitable data type. If that's the case, it is likely not written as `sin x` at all, in Haskell. So, as @leftaroundabout said, try to be clear about the involved types. – chi Jan 04 '15 at 23:35

1 Answers1

2

Use flip in conjunction with iterate:

(flip diff) "x" f = diff f "x"

so

iterate ((flip diff) "x") f = [ f, f', f'', ...]
ErikR
  • 51,541
  • 9
  • 73
  • 124
  • 3
    It would probably be a good idea to flip the definition of `diff` itself, so you can write `diff "x"` as the differential operator ∂/∂x. (Also, it would probably be better to change the type of the differentiation-variable: "magic strings" aren't very nice...) – leftaroundabout Jan 04 '15 at 20:55