functional-programming

Why does Haskell force data constructor's first letter to be upper case?

那年仲夏 提交于 2019-12-18 11:29:27
问题 Give an ugly example: data Bighead = Big little = 1 f1 = little :: Int f2 = Big :: BigHead In my opinion: f1 and f2 all point to some data. the only different of ( little and Big ) is little has a piece of code to do evaluation. but Big doesn't. They all have a rewritable body, little can be transformed from a collection of data to a result, and Big is just don't do the last step --- it always holds this data forms (but recursively they can be evaluated). But in syntax form, they are almost

Replacing if-else within 'for' loops with Java-8 Streams

泄露秘密 提交于 2019-12-18 11:26:54
问题 I have following simple code that I am trying to convert to functional style for(String str: list){ if(someCondition(str)){ list2.add(doSomeThing(str)); } else{ list2.add(doSomethingElse(str)); } } Is it easily possible to replace this loop with stream? Only option I see is to iterate over the stream twice with two different filter conditions. 回答1: It sounds like you can just use map with a condition: List<String> list2 = list .stream() .map(str -> someCondition(str) ? doSomething(str) :

Which English tutorial would you advise to learn OCaml? [closed]

女生的网名这么多〃 提交于 2019-12-18 10:41:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I want to advertise OCaml to beginners, and I am looking for good tutorials in English; not that you have only heard of, but that you have actually tried and found useful... 回答1: I quite like the book Developing Applications With Objective Caml -- I guess the title should be updated to mirror the 'OCaml' naming

Why is the use of Maybe/Option not so pervasive in Clojure?

两盒软妹~` 提交于 2019-12-18 10:32:33
问题 Why does Clojure, despite such an emphasis on functional paradigm, not use the Maybe / Option monad to represent optional values? The use of Option is quite pervasive in Scala, a functional programming language I use regularly. 回答1: Clojure is not statically typed, so doesn't need the strict this/that/whatever type declarations that are necessary in haskell (and, I gather, Scala). If you want to return a string, you return a string; if you return nil instead, that's okay too. "Functional"

In what areas does F# make “absolute no sense in using”? [closed]

时光怂恿深爱的人放手 提交于 2019-12-18 10:31:36
问题 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 . Don Syme in his SPLASH talk says that F# is NOT intended to be a replacement for C# even though it has the general capabilities. He goes on to say that there are areas where F# makes no sense in using, but doesn't expand on the thesis. Can somebody please tell me what areas

A simple example showing that IO doesn't satisfy the monad laws?

你离开我真会死。 提交于 2019-12-18 10:16:30
问题 I've seen mentioned that IO doesn't satisfy the monad laws, but I didn't find a simple example showing that. Anybody knows an example? Thanks. Edit: As ertes and n.m. pointed out, using seq is a bit illegal as it can make any monad fail the laws (combined with undefined ). Since undefined may be viewed as a non-terminating computation, it's perfectly fine to use it. So the revised question is: Anybody knows an example showing that IO fails to satisfy the monad laws, without using seq ? (Or

What is a 'thunk', as used in Scheme or in general?

瘦欲@ 提交于 2019-12-18 10:14:44
问题 I come across the word 'thunk' at a lot of places in code and documentation related to Scheme, and similar territories. I am guessing that it is a generic name for a procedure, which has a single formal argument. Is that correct? If yes, is there more to it? If no, please? For eg. in SRFI 18, in the 'Procedures' section. 回答1: It is really simple. When you have some computation, like adding 3 to 5, in your program, then creating a thunk of it means not to calculate it directly, but instead

Dynamic programming in the functional paradigm

允我心安 提交于 2019-12-18 10:13:24
问题 I'm looking at Problem thirty one on Project Euler, which asks, how many different ways are there of making £2 using any number of coins of 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). There are recursive solutions, such as this one in Scala (credit to Pavel Fatin) def f(ms: List[Int], n: Int): Int = ms match { case h :: t => if (h > n) 0 else if (n == h) 1 else f(ms, n - h) + f(t, n) case _ => 0 } val r = f(List(1, 2, 5, 10, 20, 50, 100, 200), 200) and although it runs fast enough, it

If Java people go to Scala, C# go to F#, where do Ruby people go for functional nirvana? [closed]

我是研究僧i 提交于 2019-12-18 10:12:09
问题 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 know a lot of Java people have started looking at Scala since it runs on the JVM, and a lot of people in the Microsoft world are

Why should I use applicative functors in functional programming?

我怕爱的太早我们不能终老 提交于 2019-12-18 10:09:10
问题 I'm new to Haskell, and I'm reading about functors and applicative functors. Ok, I understand functors and how I can use them, but I don't understand why applicative functors are useful and how I can use them in Haskell. Can you explain to me with a simple example why I need applicative functors? 回答1: Applicative functors are a construction that provides the midpoint between functors and monads, and are therefore more widespread than monads, while more useful than functors. Normally you can