I have heard few talks on modern programming languages like scala (and few other languages whose names I cannot remember right now), and often got the sense of excitement when the speakers were talking like: "if is an expression in our programming language, it returns value, WOW". So the question is, why an when if being an expression rather than a statement like in C is better?
Why "if as expression not statement" is cool?
455 views Asked by exebook At
2
There are 2 answers
0
Ingo
On
It may be surprising for some, but there are languages that don't have "statements" at all. One example would be Haskell. In such circumstances there is no choice than having if expressions.
Note that any decent imperative language has if expressions, too. It's just called and written differently, like, for example:
cond ? "yes" : "no"
Incidentally, imperative languages short circuit such an expression, which is slang for lazy evaluation of the 2nd and 3rd expression (just like Haskell does it anyway).
Related Questions in SCALA
- Mocking AmazonS3 listObjects function in scala
- Last SPARK Task taking forever to complete
- How to upload a native scala project to local repo by sbt like using "maven install"
- Folding a list of OR clauses in io.getquill
- How to get latest modified file using scala from a folder in HDFS
- Enforce type bound for inferred type parameter in pattern matching
- can't write pyspark dataframe to parquet file on windows
- spark streaming and kafka integration dependency problem
- how to generate fresh singleton literal type in scala using macros
- exception during macro expansion: type T is not a class, play json
- Is there any benefit of converting a List to a LazyList in Scala?
- Get all records within a window in spark structured streaming
- sbt publishLocal of a project with provided dependencies in build.sbt doesn't make these dependencies visible to projects using the project as library
- Scala composition of partially-applied functions
- How to read the input json using a schema file and populate default value if column not being found in scala?
Related Questions in IF-STATEMENT
- Basic Python Question: Shortening If Statements
- Why if condition work but else if condition doesn't work?
- How to list several items in the dialog box for execution?
- "if contains" with forbidden special characters
- bash if equals some file
- change binary data like "111 into 001" in python by using if else or using regex
- How much of a bonus do I get if I sell a specific number
- Conditional Concatenation for Exchange Contact Imports
- How can i correct a multiple choice question with multiple correct answers?
- IF AND OR Nested ArrayFormula not working - What am I missing?
- I can search one sheet, but can't manage to string two searches with the IFS function
- How to add 1 (or more generally, perform a simple operation) on a column based on a condition
- Playwright checking the text of a locator and replacing values
- Read a variable and printing it on a monitor - Arduino
- Comparison between two strings is not working
Related Questions in FUNCTIONAL-PROGRAMMING
- On Google Sheets (and only built-in functions allowed, no Google Apps Script) Is it possible to simulate pipe function?
- Why does Enum require to implement toEnum and fromEnum, if that's not enough for types larger than Int?
- Is there a functional way to map a list (N elements) to a list of sums of adjacent elements (N - 1 elements) in Kotlin?
- How to count the occurences of every element in a list in Haskell fast?
- Combine lists with absolute index in functional programming
- How to refactor a loop with iterator. (Returning from closure)
- In Haskell, what does `Con Int` mean?
- Setting up different Java class fields value by a single value on some counter value
- Why doesn't map read show (Integer) work to separate each value in a string of Integers?
- Grouping by multiple fields and counting using in Java 8
- Variable capture: How variables behave in function closures
- Composing React Providers with Value props in Typescript
- How can atomicModifyIORef cause leaks? And why does atomicModifyIORef' solve the problem?
- How can I change XMobar's Kbd monitor plugin such that clicking on it loops throught the layouts?
- How to get success or error data without folding the response while using fpdart in flutter?
Related Questions in SEMANTICS
- Perceived changes in the last decade (or more?) with respect to the semantics of char** and char*[] declarations in the c language
- Shared triples between two knowledge graphs
- eval vs Function() return semantics
- Is a hidden heading in the <footer> needed for accessibility?
- How to make talkback read out Text and switch together in Android?
- How Vespa addresses memory limitations in big data applications
- Custom HLSL Structure
- Inconsistency in application of the increment operator when used in expressions
- Is it a semantic or syntax error to not have an argument list after a name prefixed by the template keyword?
- For..of loops confusion
- showSemanticsDebugger does not work anymore
- Flutter, automatically add Semantics value to all Text Widgets
- Are there any scenarios where it would be appropriate to use an H1 in a document after an H1 has already been used?
- TextField onValueChange - Announce Additional Semantics
- Semantics (Subtyping)
Related Questions in TRADEOFF
- subjective question: when to use multi master db setup vs sharding
- Class memoization vs static class
- Should I place a public static method in a class or an interface?
- Finding the best bandwidth for kernel smoothing regression in R
- Trade off between losses?
- Technical term for describing an algorithm that "exposes trade-off between locally and globally optimal decisions"
- Alert Based on color of line
- Why computing the mse step by step don't yield the same solution as MSE function?
- Merging k sorted lists in Python3, problem with trade-off between memory and time
- Trade off between Linear and Binary Search
- Advantages of firestore sub-collections
- Which of the following code have lesser time complexity? Please explain how to calculate it
- data population and manipulation in frontend vs. backend
- Trading run-time for higher fit accuracy?
- Is std::find suitable only for containers whose elements may be not sorted?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
It has to do with dealing with values instead of dealing with (re)assignments. Values are simple and (re)assignments are hard; Consider java where if construct is a statement, and also blocks {} are also sort of statements, and try is also kind of statement, and neither of them could return value:
compare with scala, where all those statements are actually expressions and can return values:
Now I argue that scala version is plainly simpler for understanding)))
someObjectis value so it got assigned one time, and I'm not bothered that somewhere below it got reassigned. I don't have to move variables outsideifandtryblocks just to keep them visible to the outside. And if I have a lot of nested if blocks, I still can deduce what result value will be at each level just by looking at it, while in java I need to keep whole method, and in fact execute it in my head to be able to deduce what top level variables got updated and when. BTW java has?: operatorbut it is very limited compared to scala's if expression (mainly because you can't use brakets within).