functional-programming

How to use let declarations as expressions?

霸气de小男生 提交于 2020-01-17 07:21:41
问题 I want to use let expressions, but the following code doesn't work: true ? (let x=1, let y=2, x+y) : (let x=3, let y=4, x-y); // SyntaxError How am I supposed to do this? 回答1: Unfortunately, Javascript lacks lisp-style let expressions. However, since there are default parameters and arrow functions we can mimic them: const let_ = f => f(); console.log( true ? let_((x = 1, y = 2) => x + y) : let_((x = 3, y = 4) => x - y) // 3 ); To be honest, this is a bit tedious and the code is pretty

Python function composition (max recursion depth error, scope?)

房东的猫 提交于 2020-01-17 01:10:36
问题 What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note: the question is: why is it even recursing, not why it's reaching the max depth... def comp(*funcs): if len(funcs) in (0,1): raise ValueError('need at least two functions to compose') # get most inner function composed = [] print("appending func 1")

Python function composition (max recursion depth error, scope?)

纵然是瞬间 提交于 2020-01-17 01:10:32
问题 What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note: the question is: why is it even recursing, not why it's reaching the max depth... def comp(*funcs): if len(funcs) in (0,1): raise ValueError('need at least two functions to compose') # get most inner function composed = [] print("appending func 1")

Finding the first duplicate element in a list

只谈情不闲聊 提交于 2020-01-16 13:23:58
问题 I am very new to Haskell. I am trying to write code in Haskell that finds the first duplicate element from the list, and if it does not have the duplicate elements gives the message no duplicates. I know i can do it through nub function but i am trying to do it without it. 回答1: This is one way to do it: import qualified Data.Set as Set dup :: Ord a => [a] -> Maybe a dup xs = dup' xs Set.empty where dup' [] _ = Nothing dup' (x:xs) s = if Set.member x s then Just x else dup' xs (Set.insert x s)

Problem with recursion using lambda calculus (using church numerals) in Javascript

两盒软妹~` 提交于 2020-01-16 09:07:11
问题 I have been playing around with with lambda calculus in javascript (node). I created some Church numerals, and I've been trying to create a recursive function that calculates the fibonacci sequence, but it's definitely not working! I have tried wrapping the function in a Y combinator, and a Z combinator, but neither (or my application for them) worked. What I think might be happening is that javascript is just applying the recursive function and then each time it does that, the recursive

Complex mapping of array to object in Ruby

我与影子孤独终老i 提交于 2020-01-16 06:40:08
问题 I have an array of strings: ["username", "String", "password", "String"] And I want to convert this array to a list of Field objects: class Field attr_reader :name, :type def initialize(name, type) @name = name @type = type end end So I need to map "username", "String" => Field.new("username", "String") and so on. The length of the array will always be a multiple of two. Does anyone know if this is possible using a map style method call? 回答1: 1.8.6: require 'enumerator' result = [] arr = [

scala pass type parameter function as a parameter of another function

牧云@^-^@ 提交于 2020-01-16 06:09:49
问题 Suppose,I have a function, take two values and a function as parameters. def ls[S](a: S, b: S)(implicit evl: S => Ordered[S]): Boolean = a < b def myfunction[T](a: T, b: T, f:(T,T)=>Boolean) = { if (f(a, b)) { println("is ok") } else { println("not ok") } } myfunction(1, 2, ls) the IDE don't give any error message,but when I try to compile and run,the compliter give this message: Error:(14, 19) No implicit view available from S => Ordered[S]. myfunction(1, 2, ls);} ^ So,is there a way to pass

C++: Syntax for map where Data type is function?

杀马特。学长 韩版系。学妹 提交于 2020-01-16 04:20:27
问题 In C#, what I want would look something like this: IDictionary<string, action()> dict = new Dictionary<string, action()>(); How do I do this in C++? This gives compiler errors: map<string, void()> exercises; 回答1: Use boost::function, a polymorphous wrapper for any object that can be called with your signature (including functions, function objects etc). map<string, function<void()>> ...; Note that the new C++ standard has already included function in <functional> . To explain the backgrounds:

Guidance on very shallow embedding VHDL in AGDA

孤者浪人 提交于 2020-01-15 08:57:12
问题 for my project in Programming Languages I am doing a very shallow and simple embedding VHDL digital circuits in agda. The aim is to write the syntax, static semantics, dynamic semantics and then write some proofs to show our understanding of the material. Up till now I have written the following code: data Ckt : Set where var : String → Ckt bool : Bool → Ckt empty : Ckt gate : String → ℕ → ℕ → Ckt -- name in out series : String → Ckt → Ckt → Ckt -- name ckt1 ckt2 parallel : String → Ckt → Ckt

Guidance on very shallow embedding VHDL in AGDA

[亡魂溺海] 提交于 2020-01-15 08:57:02
问题 for my project in Programming Languages I am doing a very shallow and simple embedding VHDL digital circuits in agda. The aim is to write the syntax, static semantics, dynamic semantics and then write some proofs to show our understanding of the material. Up till now I have written the following code: data Ckt : Set where var : String → Ckt bool : Bool → Ckt empty : Ckt gate : String → ℕ → ℕ → Ckt -- name in out series : String → Ckt → Ckt → Ckt -- name ckt1 ckt2 parallel : String → Ckt → Ckt