functional-programming

for/continue in scheme/lisp

扶醉桌前 提交于 2021-02-19 01:14:03
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

How does the presence of the “error” function bear on the purity of Haskell?

倾然丶 夕夏残阳落幕 提交于 2021-02-18 22:09:48
问题 I've always wondered how the Haskell exception system fits in with the whole "Pure functional language" thing. For example see the below GHCi session. GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help Prelude> head [] *** Exception: Prelude.head: empty list Prelude> :t head head :: [a] -> a Prelude> :t error error :: [Char] -> a Prelude> error "ranch" *** Exception: ranch CallStack (from HasCallStack): error, called at <interactive>:4:1 in interactive:Ghci1 Prelude> The type of

Can every functional language be lazy?

允我心安 提交于 2021-02-18 21:29:33
问题 In a functional language, functions are first class citizens and thus calling them is not the only thing I can do. I can also store them away. Now when I have a language, which is strict by default, then I am still not forced to evaluate a function call. I have the option to store the function and its parameters e.g. in a tuple for later evaluation. So instead of x = f a b c I do something like x = (f,a,b,c) And later, I can evaluate this thing with something like eval (f,a,b,c) = f a b c

Functional-style JavaScript: good practice to avoid argument mutation?

风流意气都作罢 提交于 2021-02-18 11:16:11
问题 This is a rather general question. Functional-style programming promotes the idea that a program is about transforming data through functions, and that mutation should be avoided (except possibly within a function, seen as a basic unit of abstraction). But in this program: function foo (bar) { bar.k1 = "bananas"; return bar; } var o = { k1: "apples", k2: "oranges"}; var p = foo(o); the external variable o is mutated within foo because bar is a reference to o, and, in the end, o === p (they

Zip elements with odd and even indices in a list

你离开我真会死。 提交于 2021-02-18 11:08:41
问题 I want to zip even and odd elements in a list to make a list of pairs, like that: ["A", "B", "C", "D", "E", "F"] -> [("A", "B"), ("C", "D"), ("E", "F")] What is the most concise expression to do this in elegant in functional way? 回答1: In 2.8, you'd probably use methods: scala> val a = "ABCDEF".toList.map(_.toString) a: List[java.lang.String] = List(A, B, C, D, E, F) scala> a.grouped(2).partialMap{ case List(a,b) => (a,b) }.toList res0: List[(java.lang.String, java.lang.String)] = List((A,B),

Automatically figuring out function name in f#

我是研究僧i 提交于 2021-02-18 11:00:28
问题 If I have a function that is part of a module, and I want a log entry while inside the function,I have to manually print the function namespace and name e.g. namespace MyModuleNamespace module MyModule = let AddTwoNums logger x y = logger.Info("MyModuleNamespace.AddTwoNums - Start") let res = x+y logger.Info("MyModuleNamespace.AddTwoNums - End") res Is there any way I can automatically work out what "MyModuleNamespace.AddTwoNums" is as it is very cumbersome/error prone especially when you

Modify dict values inplace

懵懂的女人 提交于 2021-02-17 19:16:12
问题 I would like to apply a function to values of a dict inplace in the dict (like map in a functional programming setting). Let's say I have this dict : d = { 'a':2, 'b':3 } I want to apply the function divide by 2.0 to all values of the dict, leading to: d = { 'a':1., 'b':1.5 } What is the simplest way to do that? I use Python 3 . Edit: A one-liner would be nice. The divide by 2 is just an example, I need the function to be a parameter. 回答1: You may find multiply is still faster than dividing

Java 8 collecting the list that is already present in object

醉酒当歌 提交于 2021-02-17 03:44:07
问题 I was just searching for better way to handle this scenario using java 8 streams. Object A has list of object b. What I get is a list of object A (List). I need to stream through list of object A and get all the listB's in each of the object A as a one single list. class A { List<B> listB } I have tried the below way it throws compilation List<A> as = someObject.getAs(); List<B> listofBs = as.stream().map(in -> in.getListB()).collect(Collectors.toList()); 回答1: To get a single list of all B's,

Haskell Is there a function for creating every variation of applying a function to a list

与世无争的帅哥 提交于 2021-02-16 18:54:05
问题 I want to create a list of variations of applying a function to every element of a list. Here is a quick example of what I mean. applyVar f [a, b, c] >> [[(f a), b, c], [a, (f b), c], [a, b, (f c)]] Essentially It applies a function to each element of a list individually and stores each possible application in an array. I'm not too sure how to approach a problem like this without using indexes as I have heard they are not very efficient. This is assuming that the function f returns the same

Scala Kleisli throws an error in IntelliJ

人盡茶涼 提交于 2021-02-13 12:27:55
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>