imperative-programming

Check if a value still remains the same in a While loop Python

爷,独闯天下 提交于 2021-02-17 03:38:12
问题 I want know if there is a elegant method for looking if a value that continually changes in a while loop can be checked and stop the while loop if the value stops change and remains the same. For example: Value = 0 while True: value changes everytime (if value still the same break) 回答1: How about this way? BTW: Fix your typo error while is not While in python. value = 0 while True: old_value, value = value, way_to_new_value if value == old_value: break 回答2: previous = None current = object()

Mixing object-oriented and functional programming

与世无争的帅哥 提交于 2019-12-29 16:26:41
问题 What languages are available that promote both object-oriented and functional programming? I know that any language that supports first-class functions can be considered functional, but I'm looking for a syntax that's specifically targeted for both coding styles. Using such a language, I'm imagining isolating all state changes to a single portion of code and having the rest of the program be purely functional. Just thinking of it makes me drool (debugging heaven!). So far I've discovered

Declarative Language [closed]

放肆的年华 提交于 2019-12-13 23:13:21
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I was reading an article on Declarative Programming Languages. If I don't understand the qualities of this type/paradigm of

Stateful computation with different types of short-circuit (Maybe, Either)

天涯浪子 提交于 2019-12-13 14:22:56
问题 I am trying to find the most elegant way of converting the following stateful imperative piece of code to pure functional representation (preferably in Haskell to use abstraction that its Monad implementation offers). However I am not yet good at combining different monads using transformers and the like. It seems to me, that analyzing other's takes on such tasks helps the best when learning how to do it myself. The imperative code: while (true) { while (x = get()) { // Think of this as

Convert recursive binary tree traversal to iterative

风格不统一 提交于 2019-12-12 09:52:54
问题 I was asked to write the iterative version, but I wrote the recursive version i.e. void inorderTraverse(BinaryTree root) { if(root==NULL) printf("%d",root->id); else { inorderTraverse(root->left); printf("%d",root->id); inorderTraverse(root->right); } } I'm not looking for the code, I want to understand how this can be done. Had it been just the last recursive call, I would have done void inorderTraverse(BinaryTree root) { while(root!=NULL) { printf("%d",root->id); root=root->right; } } But

Possible OCaml code generation bug

£可爱£侵袭症+ 提交于 2019-12-12 03:49:33
问题 The following self contained code highlights a problem in OCaml, possibly with the code generation. Array x has connectivity information for nodes in [0..9]. Function init_graph originally constructed explicit arrays of incoming nodes for every node. The reduced version shown below just prints the two connected nodes. Function init_graph2 is identical to init_graph except for a "useless" else branch. But outputs produced by these two functions are quite different. You can run it and see that

Functional programming style vs performance in Ruby [closed]

纵然是瞬间 提交于 2019-12-11 03:42:35
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I love functional programming and I love Ruby as well. If I can code an algorithm in a functional style rather than in a imperative style, I do it. I tend to do not update or reuse variables as much as possible, avoid using "bang!" methods and use "map", "reduce", and similar

Haskell vs. procedural programming in the real world [closed]

坚强是说给别人听的谎言 提交于 2019-12-08 22:53:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . These days I'm getting seriously into functional programming. While I'm really excited about Haskell and the possibilities it seems to offer, I can also see now that it is going to take me a while to learn. In an SO question on How to learn Haskell an answer states that it'll

Are programs in functional languages more likely to have stack overflows?

我怕爱的太早我们不能终老 提交于 2019-12-07 07:41:49
问题 I am starting to learn ocaml, and am really appreciating the power of recursion in the language. However, one thing that I am worried about is stack overflows. If ocaml uses the stack for function calls, won't it eventually overflow the stack? For example, if I have the following function: let rec sum x = if x > 1 then f(x - 1) + x else x;; it must eventually cause a stack-overflow. If I was to do the equivalent thing in c++ (using recursion), I know that it would overflow. So my question is,