language-design

Function arguments (in Python for example)

余生长醉 提交于 2019-12-13 11:19:06
问题 What are [function] arguments? What are they used for? I started learning Python very recently; I'm new to programming and I apologize for this basic question. In every Python tutorial I go through they talk about arguments . I have looked for the answer to this question and have found many answers but they are just a little too hard for me to understatnd. I may just be missing some conceptual background. So... when I define a function, what are the things in parenthesis used for? Example:

Function arguments (in Python for example)

坚强是说给别人听的谎言 提交于 2019-12-13 09:53:33
问题 What are [function] arguments? What are they used for? I started learning Python very recently; I'm new to programming and I apologize for this basic question. In every Python tutorial I go through they talk about arguments . I have looked for the answer to this question and have found many answers but they are just a little too hard for me to understatnd. I may just be missing some conceptual background. So... when I define a function, what are the things in parenthesis used for? Example:

How to bring an interpreter to the IO monad?

微笑、不失礼 提交于 2019-12-13 09:06:49
问题 My question relates to the simple interpreter written in in this answer How could one add IO capabilities to this interpreter (the first non monadic version)? By this I simply mean adding a statement that uses putStrLn. I'm not that well versed in Haskell yet, but I'm guessing you can just combine the IO monad somehow. Can somebody point me in the right direction? data Stmt = Var := Exp | While Exp Stmt | Seq [Stmt] | Print Exp -- a print statement 回答1: You can make your interpreter have a

“Least Astonishment” and the Mutable Default Argument

﹥>﹥吖頭↗ 提交于 2019-12-13 07:14:45
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it "a dramatic design flaw"

How to write abstract class constructors so that it will be flexible for extending in sub classes

江枫思渺然 提交于 2019-12-13 00:38:28
问题 I am trying to implement a persistent Stack data structure. I want to implement this as an algebraic data type, so it has two concrete subtypes: empty and non empty : abstract class Stack<T> { factory Stack.empty() => const _EmptyStack._(); T get data; Stack<T> get bottom; bool get isEmpty; Stack<T> put(T item) => new _StackImpl(item, this); } class _StackImpl<T> extends Stack<T> { final T _data; final Stack<T> _bottom; _StackImpl(T this._data, Stack<T> this._bottom); T get data => _data;

Design pattern as (missing) language feature

做~自己de王妃 提交于 2019-12-12 10:42:43
问题 Sometimes people refer to design patterns as missing programming language features. To avoid the debate about what is a design pattern, let's say we only consider the original GoF patterns. For instance, the singleton pattern vanishes in Scala which supports singleton objects using the keyword object . There are few resources around about this, notably Are Design Patterns Missing Language Features from the C2 wiki, or Are design patterns really language weaknesses? from SO. But I couldn't

Has the C# spec (team? committee?) ever considered this object creation syntax?

随声附和 提交于 2019-12-12 10:31:35
问题 I've never posted a question of this nature before, so if it's not proper for SO, just don't hurt my feelings too bad and I'll delete it. In the interest of keeping everything I care about as close to the left margin as possible, I keep wishing I could write something like: DataService1.DataEntities dataEntities = new(constructorArg1, ...) I think another reason is I like the extra screen real estate I get by using var when the type is already present on the right side of the assignment, but

Why can't Regular Expressions use keywords instead of characters?

送分小仙女□ 提交于 2019-12-12 08:23:35
问题 Okay, I barely understand RegEx basics, but why couldn't they design it to use keywords (like SQL) instead of some cryptic wildcard characters and symbols? Is it for performance since the RegEx is interpreted/parsed at runtime? (not compiled) Or maybe for speed of writing? Considering that when you learn some "simple" character combinations it becomes easier to type 1 character instead of a keyword? 回答1: You really want this ? Pattern findGamesPattern = Pattern.With.Literal(@"<div")

Why does C++ enforce such behavior in crosses initialization? [duplicate]

天涯浪子 提交于 2019-12-12 08:22:07
问题 This question already has answers here : Closed 7 years ago . Let's say I have some c++ code: if (error) goto exit; ... // size_t i = 0; //error size_t i; i = 0; ... exit: ... I understand we should not use goto , but still why does size_t i; i = 0; compile whereas size_t i = 0; doesn't? Why is such behavior enforced by the standard (mentioned by @SingerOfTheFall)? It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps

Are there any more elegant ways of handling lists in Java ? (Python VS Java)

元气小坏坏 提交于 2019-12-12 07:59:35
问题 I do like the way I can treat lists in Python. It does any recursion solution to look easy and clean. For instance the typical problem of getting all the permutations of elements in a list, in Python looks like: def permutation_recursion(numbers,sol): if not numbers: print "this is a permutation", sol for i in range(len(numbers)): permutation_recursion(numbers[:i] + numbers[i+1:], sol + [numbers[i]]) def get_permutations(numbers): permutation_recursion(numbers,list()) if __name__ == "__main__