Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I'm just starting to learn Mathematica and I got stuck at this question. This a noob question but please help.

I need to define a function named continuedFraction that takes user input of type list as this continuedFraction[{a,b,c,d}] that will output

a / (1 + b/ (1 + c/ (1 + d)))

this is just any other continued fraction like on this page

what I had done so far after 383 inputs in notebook (now you know how hard this is for me)

fx[x_] := Fold[x/(1 + #) &, x, {a, b, c,}]

fx[{a, b, c, e}]

 {a/(1 + a/(1 + a/(1 + a/(1 + a)))), 
  b/(1 + b/(1 + b/(1 + b/(1 + b)))), 
  c/(1 + c/(1 + c/(1 + c/(1 + c)))), 
  e/(1 + e/(1 + e/(1 + e/(1 + e))))}

which is of course, not the correct answer. ps: this is under functional programming-> pure function and a hint to use Fold.

share|improve this question

marked as duplicate by J. M. May 21 '13 at 14:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Why not use FromContinuedFraction? – Michael E2 May 21 '13 at 14:16
because I am asked to create a function of my own definition. – momokjaaaaa May 21 '13 at 15:20

1 Answer 1

up vote 6 down vote accepted

You're getting close. (Fold is an excellent choice.) Try:

f[x_List] := Fold[#2/(1 + #) &, 0, Reverse@x]

f[{a, b, c, d}]
a/(1 + b/(1 + c/(1 + d)))
share|improve this answer
Suggested reading: (1), (2) – Mr.Wizard May 21 '13 at 14:10
thank you very much. how can I didn't see the #2 before! pheww~ – momokjaaaaa May 21 '13 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.