functional-programming

Tree building function in Haskell (Homework)

倖福魔咒の 提交于 2020-01-02 22:06:11
问题 data Tree a = Leaf | Node (Tree a) a (Tree a) deriving (Eq, Show) unfoldTree:: (b -> Maybe (b, a, b)) -> b -> Tree a unfoldTree f b = case f b of Nothing -> Leaf Just (lt, x, rt) -> Node (unfoldTree f lt) x (unfoldTree f rt) Given the two piece of information above, I'm asked to implement a tree building function. and my attempt is treeBuild :: Integer -> Tree Integer treeBuild 0 = Leaf treeBuild n = treeUnfold (\b -> if b < 2^n-1 then Just(2*b, b + 1, 2*b + 1) else Nothing) 0 The base case

find out if a number is a good number in scala

你离开我真会死。 提交于 2020-01-02 18:36:21
问题 Hi I am new to scala functional programming methodology. I want to input a number to my function and check if it is a good number or not. A number is a good number if its every digit is larger than the sum of digits which are on the right side of that digit. For example: 9620 is good as (2 > 0, 6 > 2+0, 9 > 6+2+0) steps I am using to solve this is 1. converting a number to string and reversing it 2. storing all digits of the reversed number as elements of a list 3. applying for loop from i

How to functional compose transforms of objects via transducers

江枫思渺然 提交于 2020-01-02 14:32:15
问题 Live code example I'm trying to learn transducers via egghead and I think I got it until we try to compose object transformation. I have an example below that doesn't work const flip = map(([k,v]) => ({[v]: k})); const double = map(([k,v]) => ({[k]: v + v})); seq(flip, {one: 1, two: 2}); /*?*/ {1: 'one', 2: 'two'} seq(double, {one: 1, two: 2}); /*?*/ {'one': 2, 'two: 4} but if I compose it fails: seq(compose(flip, double), {one: 1, two: 2}); /*?*/ {undefined: NaN} seq(compose(double, flip),

How to functional compose transforms of objects via transducers

旧城冷巷雨未停 提交于 2020-01-02 14:31:23
问题 Live code example I'm trying to learn transducers via egghead and I think I got it until we try to compose object transformation. I have an example below that doesn't work const flip = map(([k,v]) => ({[v]: k})); const double = map(([k,v]) => ({[k]: v + v})); seq(flip, {one: 1, two: 2}); /*?*/ {1: 'one', 2: 'two'} seq(double, {one: 1, two: 2}); /*?*/ {'one': 2, 'two: 4} but if I compose it fails: seq(compose(flip, double), {one: 1, two: 2}); /*?*/ {undefined: NaN} seq(compose(double, flip),

Scheme - sum of list

喜你入骨 提交于 2020-01-02 13:57:08
问题 I'm trying to implement a function which calc sum of list , its name is sum - (define (sum elemList) (if (null? elemList) (+ (car elemList) (sum (cdr elemList))) 0 ) ) The above implementation gives wrong result , for example - > (sum (list 1 2 3 4 )) 0 What I did wrong here ? 回答1: I think you swapped the then and the else part of the if : (define (sum elemList) (if (null? elemList) 0 (+ (car elemList) (sum (cdr elemList))) ) ) In the original function, for every non-empty list, 0 is returned

Javascript array from all elements with a certain class name

╄→尐↘猪︶ㄣ 提交于 2020-01-02 10:06:03
问题 I am trying to make an array from elements with a certain class in my web page. The array should get the videofile attribute value from all a tags with the class videoLink. The final values in the array should be. cycling_large, ocean_medium, winecountry_part1 <a class="videoLink" videofile="cycling_large" ></a> <a class="videoLink" videofile="ocean_medium" ></a> <a class="videoLink" videofile="winecountry_part1" ></a> I tried this but, does not work. var values = $('.videoLink').map(function

Determining a total, terminating function

二次信任 提交于 2020-01-02 09:57:42
问题 I am trying to determine whether for the following type: ((a -> c) -> c) -> a a total, terminating function can be written using that type as a function signature? I understand that in order for a function to be total it has to be defined for all possible values of its input. However, I am not sure what exactly does it means for a function to be terminating. For a function to be terminating, does it have to return a value (and not go into an infinite loop for example)? Further, what methods

Creating an Enumeratee from a stateful algorithm

蹲街弑〆低调 提交于 2020-01-02 09:56:30
问题 I have a stateful algorithm that incrementally takes input and incrementally produces output. The inputs and outputs are unrelated in number; i.e. an input may produce zero or more outputs. I am attempting to turn it into an Enumeratee in the Play Framework, but I am having difficulty getting started. My algorithm has local mutable state and synchronous operations and looks something like this var state = 'foo var i = input() while (i != null) { if (state == 'foo && i >= 0) { // 2 outputs,

Is it possible to build a Reactive Application using a non-Functional language?

依然范特西╮ 提交于 2020-01-02 07:12:22
问题 I would like to understand if the principles behind the Reactive Application manifesto can be achieved using a non-functional language . Some people say that since FP use immutable states and free side-effects functions, they are easier to implement concurrent, distributed and resilient systems. But how can we achieve that using Java for example? There are some frameworks like Apache Camel, that have some components to work with, like Camel RX , and Camel SEDA . Are these frameworks enough? I

Type sharing in OCaml - typechecker error

江枫思渺然 提交于 2020-01-02 07:07:14
问题 When compiling this program: module type Inc = sig type t val inc : t -> t end module type Dec = sig type t val dec : t -> t end module Merger (I : Inc) (D : Dec with type t = I.t) = struct let merge x = x |> I.inc |> D.dec end module IntInc : Inc = struct type t = int let inc x = x + 10 end module IntDec : Dec = struct type t = int let dec x = x - 8 end module Combiner = Merger (IntInc) (IntDec) I get the following error: File "simple.ml", line 30, characters 35-41: Error: Signature mismatch