functional-programming

How to order my accumulate variable in this case on Racket?

陌路散爱 提交于 2019-12-20 04:36:31
问题 I am coding with Racket for educational reasons. I was given a task in which I should create a function that, without filter, would receive a list as an input and return another list only with the even numbers of the first list. I came up with this recursive definition of an iterative process: (define (add-even lista) (define (iter lista accu) (cond ((null? lista) accu) ((even? (car lista)) (iter (cdr lista) (cons (car lista) accu))) (else (iter (cdr lista) accu)))) (iter lista empty)) It

Haskell: What is immutable data?

ⅰ亾dé卋堺 提交于 2019-12-20 04:25:10
问题 in most articles about Haskell you'll find a statement like "Data in Haskell is immutable". I don't quite understand why. For example: let a = 123 let a = 456 in the main method works. I just changed the data of a from 123 to 456 . What am I missing? It's probably a stupid mistake in my train of thought :/ Have a good day! 回答1: Actually, a hasn't changed. Try this in ghci to see: > a = 123 > action = print a > a = 456 > action 123 Compare with a language that has mutable variables, e.g.

Strange module loading issue in OCaml

折月煮酒 提交于 2019-12-20 03:52:50
问题 I have two files: myUnionFind.ml and myUnionFind_test.ml . Both files are in the same directory . myUnionFind.ml open Batteries module type MyUnionFindSig = sig type union_find val print_array : 'a array -> unit val create_union : int -> union_find val union_weighted : union_find -> int -> int -> unit val is_connected_weighted : union_find -> int -> int -> bool end;; module MyUnionFind : MyUnionFindSig = struct let print_array ary = print_endline (BatPervasives.dump ary);; type union_find =

Working with Sets as Functions

我的未来我决定 提交于 2019-12-20 03:29:09
问题 From a FP course: type Set = Int => Boolean // Predicate /** * Indicates whether a set contains a given element. */ def contains(s: Set, elem: Int): Boolean = s(elem) Why would that make sense? assert(contains(x => true, 100)) Basically what it does is provide the value 100 to the function x => true . I.e., we provide 100, it returns true . But how is this related to sets? Whatever we put, it returns true . Where is the sense of it? I understand that we can provide our own set implementation

How to unnest these Promises?

本秂侑毒 提交于 2019-12-20 03:28:44
问题 Background I have a function that makes a request to a server. If the request fails, I want to: 1. log the error 2. run a terminal command 2.1 log if the command failed or succeeded To achieve this I have the following code: const createRequest = ( { request, logger, terminal } ) => ( { endpoint, timeout } ) => request.get( endpoint, { timeout } ) .then( response => logger.info( { event: "Heartbeat request succeeded.", status: response.status } ) ) .catch( err => logger.error( { event:

What am I doing wrong with Set.Fold F#

怎甘沉沦 提交于 2019-12-20 03:26:04
问题 Colouring problem : Hello, I'm trying to implement a bool function that returns true when a color can be extended to a country and false otherwise but I'm having trouble working with sets as we cannot pattern match them... My code : type Country = string;; type Chart = Set<Country*Country>;; type Colour = Set<Country>;; type Colouring = Set<Colour>;; (* This is how you tell that two countries are neghbours. It requires a chart.*) let areNeighbours ct1 ct2 chart = Set.contains (ct1,ct2) chart

Python deep zip

允我心安 提交于 2019-12-20 03:21:48
问题 I am trying to write a function like zip. I am not good at explaining what I mean, so i will just show 'code' of what i'm trying to do. a = [1,2,3,[4,5]] b = a[:] zip(a, b) == [(1,1), (2,2), (3,3), ([4,5],[4,5])] myzip(a, b) == [(1,1), (2,2), (3,3), [(4,4), (5,5)]] I am so stuck on this it's not even funny. I am trying to write it in a simple functional way with recursive lambdas, to make my code prettier. I want myzip like this because i want to use its output with another function I wrote

Combining std::function objects

可紊 提交于 2019-12-20 03:21:37
问题 Say I have double xSquared( const double ) { return x*x; } ... std::function<double (double)> func = &xSquared; ... which works fine for the (more complicated) purposes I use this structure, up till now. Now I have a function that accepts a std::function of the above form and I need to create a new std::function that extends the original: typedef std::function<double (double)> func1D; double someFunction( const func1D &func, const double a ) { func1D extendedFunc = func/(x-a); // I know this

Combining std::function objects

核能气质少年 提交于 2019-12-20 03:21:08
问题 Say I have double xSquared( const double ) { return x*x; } ... std::function<double (double)> func = &xSquared; ... which works fine for the (more complicated) purposes I use this structure, up till now. Now I have a function that accepts a std::function of the above form and I need to create a new std::function that extends the original: typedef std::function<double (double)> func1D; double someFunction( const func1D &func, const double a ) { func1D extendedFunc = func/(x-a); // I know this

How to combine equal sequence elements (functional programming)?

妖精的绣舞 提交于 2019-12-20 02:44:31
问题 I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>. I'm using sequences, not lists, but some of the functions are similar. Some of the functions I am thinking of using are map, reduce, tabulate, filter, append etc.. Reduce takes in an associative function and returns the sequence that is "reduced" by that operator. So, reduce op+ 0 <1,2,3> = 6. My first thought was to use map to raise the sequence by