functional-programming

BigInt for Standard ML/NJ

半城伤御伤魂 提交于 2019-12-21 18:02:54
问题 Is there a Java BigInt equivalent for Standard ML? The normal int type throws an exception when it overflows. 回答1: Yes, see the IntInf structure. 回答2: The official SML'97 standard basis library introduces a zoo of structures like Int, IntInf, Int32, Int64, LargeInt etc. To actually use them in practice to make things work as expected, and make them work efficiently, you need to look closely at the SML implementation at hand. One family of implementations imitates the memory layout of C and

java8 stream style for retrieving a inner part of map through a field list?

蹲街弑〆低调 提交于 2019-12-21 18:00:30
问题 For example, given a map like below: { "k1": { "k2": { "k3": { "k4": "v" } } } } and a field list ["k1","k2","k3"] , I need to retrieve the part {"k4": "v"} . Below is my java7-style code: // Ignore the map building code. Map map1 = new HashMap(); Map map2 = new HashMap(); Map map3 = new HashMap(); Map map4 = new HashMap(); map4.put("k4", "v"); map3.put("k3", map4); map2.put("k2", map3); map1.put("k1", map2); Map map = map1; System.out.println(map); //=> {k1={k2={k3={k4=v}}}} // Code to be

Attempting to use continuation passing style to avoid stack overflow with minimax algorithm

白昼怎懂夜的黑 提交于 2019-12-21 17:48:09
问题 Summary of my objective: Figure out how to use continuation-passing style to avoid a stack overflow when using an algorithm I believe cannot be made tail-recursive. Alternatively, find a way to make the function tail-recursive. Details: I am new to F# (and functional programming in general) and I am attempting to implement the minimax algorithm with alpha-beta pruning. This is an algorithm used to determine the best possible move for a two-player game. The pseudocode for the algorithm can be

Common Lisp: compilation vs evaluation

。_饼干妹妹 提交于 2019-12-21 17:26:36
问题 On Emacs + Slime with sbcl, once I define a function (or more) in a file I have two choices: Evaluation : e.g. with C-M-x eval-defun Compilation : e.g. with C-c M-k compile-file The second one produces a .fasl file, too. What are the differences between the two? What's going on under the hood when I compile a definition / a file? What are the Pros and Cons of each one? 回答1: First of all, there's a function eval [1], that allows to evaluate (i.e. execute) arbitrary CL form in the language

Pure functional Random number generator - State monad

人走茶凉 提交于 2019-12-21 12:39:38
问题 The book ' Functional Programming in Scala ' demonstrates an example of pure functional random number generator as below trait RNG { def nextInt: (Int, RNG) } object RNG { def simple(seed: Long): RNG = new RNG { def nextInt = { val seed2 = (seed*0x5DEECE66DL + 0xBL) & ((1L << 48) - 1) ((seed2 >>> 16).asInstanceOf[Int], simple(seed2)) } } } The usage will look like val (randomNumber,nextState) = rng.nextInt I do get the part that it's a pure function as it returns the next state and leaves it

What is this functional “pattern” called?

徘徊边缘 提交于 2019-12-21 12:37:33
问题 I was fooling around with some functional programming when I came across the need for this function, however I don't know what this sort of thing is called in standard nomenclature. Anyone recognizes it? function WhatAmIDoing(args...) return function() return args end end Edit: generalized the function, it takes a variable amount of arguments ( or perhaps an implicit list) and returns a function that when invoked returns all the args, something like a curry or pickle, but it doesn't seem to

What is this functional “pattern” called?

拟墨画扇 提交于 2019-12-21 12:35:24
问题 I was fooling around with some functional programming when I came across the need for this function, however I don't know what this sort of thing is called in standard nomenclature. Anyone recognizes it? function WhatAmIDoing(args...) return function() return args end end Edit: generalized the function, it takes a variable amount of arguments ( or perhaps an implicit list) and returns a function that when invoked returns all the args, something like a curry or pickle, but it doesn't seem to

Scala filter on a list by index

不问归期 提交于 2019-12-21 12:29:16
问题 I wanted to write it functionally, and the best I could do was: list.zipWithIndex.filter((tt:Tuple2[Thing,Int])=>(tt._2%3==0)).unzip._1 to get elements 0, 3, 6,... Is there a more readable Scala idiom for this? 回答1: If efficiency is not an issue, you could do the following: list.grouped(3).map(_.head) Note that this constructs intermediate lists. Alternatively you can use a for-comprehension: for { (x,i) <- list zipWithIndex if i % 3 == 0 } yield x This is of course almost identical to your

Is recursion in scala very necessary?

我们两清 提交于 2019-12-21 07:55:37
问题 In the coursera scala tutorial, most examples are using top-down iterations. Partially, as I can see, iterations are used to avoid for/while loops. I'm from C++ and feel a little confused about this. Is iteration chosen over for/while loops? Is it practical in production? Any risk of stackoverflow? How about efficiency? How about bottom up Dynamic Programming (especially when they are not tail-recusions)? Also, should I use less "if" conditions, instead use more "case" and subclasses? 回答1:

Will I be able to use Clojure functions as Lambdas in Java 8?

寵の児 提交于 2019-12-21 07:50:31
问题 I use a number of libraries in Clojure that produce higher order functions that conform to the "clojure.lang.IFn" interface. It has multiple arity overloads, I.e. the interface looks something like: public interface IFn extends Callable, Runnable{ public Object invoke() ; public Object invoke(Object arg1) ; public Object invoke(Object arg1, Object arg2) ; public Object invoke(Object arg1, Object arg2, Object arg3) ; .... etc. public Object applyTo(ISeq arglist) ; } Will I be able to use