frege

函数柯里化详解

╄→尐↘猪︶ㄣ 提交于 2020-07-27 21:52:13
函数柯里化详解 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的新函数的技术。这个技术由 Christopher Strachey 以逻辑学家 Haskell Curry 命名的,尽管它是 Moses Schnfinkel 和 Gottlob Frege 发明的。 经典面试题: // 实现一个add方法,使计算结果能够满足如下预期: add(1)(2)(3) = 6; add(1, 2, 3)(4) = 10; add(1)(2)(3)(4)(5) = 15; function add() { // 第一次执行时,定义一个数组专门用来存储所有的参数 var _args = Array.prototype.slice.call(arguments); // 在内部声明一个函数,利用闭包的特性保存_args并收集所有的参数值 var _adder = function() { _args.push(...arguments); return _adder; }; // 利用toString隐式转换的特性,当最后执行时隐式转换,并计算最终的值返回 _adder.toString = function () { return _args.reduce(function (a, b) {

Does Haskell/Frege ever recalcuate elements of a lazy list?

删除回忆录丶 提交于 2020-01-04 03:54:08
问题 Suppose I have a list of all primes, defined as primes :: (Enum α, Integral α) => [α] primes = sieve [2..] where sieve :: (Integral α) => [α] -> [α] sieve [] = undefined sieve (x:xs) = x : (sieve $ filter ((/= 0) . (flip mod x)) xs) and I want to feed primes through multiple, different functions like: sumOfPrimesLessThan :: (Integral α) => α -> α sumOfPrimesLessThan n = sum $ takeWhile (< n) primes or productOfPrimesLessThan :: (Integral α) => α -> α productOfPrimesLessThan n = foldl (*) 1 $

Does Haskell/Frege ever recalcuate elements of a lazy list?

╄→гoц情女王★ 提交于 2020-01-04 03:54:05
问题 Suppose I have a list of all primes, defined as primes :: (Enum α, Integral α) => [α] primes = sieve [2..] where sieve :: (Integral α) => [α] -> [α] sieve [] = undefined sieve (x:xs) = x : (sieve $ filter ((/= 0) . (flip mod x)) xs) and I want to feed primes through multiple, different functions like: sumOfPrimesLessThan :: (Integral α) => α -> α sumOfPrimesLessThan n = sum $ takeWhile (< n) primes or productOfPrimesLessThan :: (Integral α) => α -> α productOfPrimesLessThan n = foldl (*) 1 $

Count inversions: StackOverflowError in Frege, works fine in Haskell

北慕城南 提交于 2019-12-23 11:58:35
问题 I am trying to count inversions for a list of numbers. The following Frege program works for small set of numbers but throws StackOverflowError for 100000 numbers. import frege.IO inversionCount [] _ = (0, []) inversionCount [x] _ = (0, [x]) inversionCount xs n = (count, sorted) where count = lcount + rcount + mergecount (lcount, lsorted) = inversionCount left lsize (rcount, rsorted) = inversionCount right rsize (mergecount, sorted) = inversionMergeCount lsorted lsize rsorted rsize (0, [])

Is there a good reason why `deleteBy` does not have its most general type?

若如初见. 提交于 2019-12-22 01:34:40
问题 The Haskell 2010 Language Report states in section 20.10.1.1 that: deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a] In fact, the implementation in the GHC library would allow deleteBy :: (b -> a -> Bool) -> b -> [a] -> [a] but actually restricts the type to the former one with the annotation. Hence, one cannot say, for instance: foo = deleteBy fsteq 42 [(43, "foo"), (44, "bar"), (42, "baz")] where fsteq a (b,_) = a == b because Int is not the same as (Int, String) . Is there any good reason

Akka with Frege running slower than Scala counterpart

。_饼干妹妹 提交于 2019-12-18 15:47:33
问题 As an exercise, I took these Scala and Java examples of Akka to port to Frege. While it works fine, it runs slower(11s) than Scala(540ms) counterpart. module mmhelloworld.akkatutorialfregecore.Pi where import mmhelloworld.akkatutorialfregecore.Akka data PiMessage = Calculate | Work {start :: Int, nrOfElements :: Int} | Result {value :: Double} | PiApproximation {pi :: Double, duration :: Duration} data Worker = private Worker where calculatePiFor :: Int -> Int -> Double calculatePiFor !start

what is the Frege equivalent to Haskell's “interact” function?

↘锁芯ラ 提交于 2019-12-14 02:11:42
问题 I try to get the word-count example from real-world Haskell running in Frege: main _ = interact wordCount where wordCount input = show (length (lines input)) ++ "\n" but I get can't resolve `interact` Is there a Frege-idiomatic way to do this? 回答1: It is not in the standard library but you can define something like this: import Data.List(intercalate) interact :: (String -> String) -> IO () interact f = stdin.getLines >>= println . f . intercalate "\n" Update (for the comment on Groovy's

How to execute a compiled code snipped in Frege online repl

穿精又带淫゛_ 提交于 2019-12-12 19:01:07
问题 OK, I guess this is a stupid beginners question: I try to learn Frege through the online repl. For doing so, I though it would be a good idea to paste code examples from Dierk's Real World Frege to the upper right window of the repl, press compile and... ? How do I start this code? 回答1: I guess I partly found the answer myself: the :java command in the command line shows the generated code. Within this code, it is visible that the compiled module is automatically imported, so we don't have to

what is the easiest way to pass a list of integers from java to a frege function?

青春壹個敷衍的年華 提交于 2019-12-11 11:56:48
问题 Assume I have a Frege module module Util where total :: [Int] -> Int total xs = fold (+) 0 xs If "total" was written in Java, I could call it via Util.total(Arrays.asList(1,2,3)); What is the best way to call the Frege implementation from Java? 回答1: You could use a good old int [] array, the corresponding frege type would be JArray Int. Because arrays can be made from and into lists in both Java and frege, they are good for such tasks. Please use the repl to get an idea how to convert the

Akka with Frege running slower than Scala counterpart

删除回忆录丶 提交于 2019-11-30 13:00:05
As an exercise, I took these Scala and Java examples of Akka to port to Frege. While it works fine, it runs slower(11s) than Scala(540ms) counterpart. module mmhelloworld.akkatutorialfregecore.Pi where import mmhelloworld.akkatutorialfregecore.Akka data PiMessage = Calculate | Work {start :: Int, nrOfElements :: Int} | Result {value :: Double} | PiApproximation {pi :: Double, duration :: Duration} data Worker = private Worker where calculatePiFor :: Int -> Int -> Double calculatePiFor !start !nrOfElements = loop start nrOfElements 0.0 f where loop !curr !n !acc f = if n == 0 then acc else loop