functional-programming

What/where is get_Zero in F#'s int?

血红的双手。 提交于 2020-01-03 13:38:55
问题 I'm just learning F#, and while playing at tryfsharp.org I noticed that if I change this code: [0..100] |> List.sum to ["A"; "B"; "D"] |> List.sum I get the following error: The type 'string' does not support the operator 'get_Zero' (Here's the script that you can run/amend in your browser, though it only seems to work in IE for me!) When I checked the definition of List.sum; it says that the type must have a static member called Zero. This seems to explain the error; except for the fact that

converting for loop to java 8 stream

旧城冷巷雨未停 提交于 2020-01-03 13:03:11
问题 I was playing around with Java 8. I had some trouble converting this for loop into Java 8 Stream. for (int y = 0; y < 5; y ++) { for (int x = y; x < 10; x += 2) { System.out.println(x+y); } } Please help! 回答1: The canonical way of converting nested loops is to use flatMap on a stream, e.g. IntStream.range(0, 5).flatMap(i->IntStream.range(i, 10)) .forEach(System.out::println); The tricky part on your task is the increment by two as this has no direct equivalent in the stream API. There are two

Getting the head and tail of a custom list type in Haskell

百般思念 提交于 2020-01-03 11:46:53
问题 I have a custom list type: data NNList a = Sing a | Append ( NNList a) ( NNList a) deriving (Eq) data CList a = Nil | NotNil ( NNList a) deriving (Eq) I'm trying to implement a function that returns the head and tail of a list: cListGet :: CList a -> Maybe (a, CList a) My attempt: cListGet :: CList a -> Maybe (a, CList a) cListGet Nil = Nothing cListGet xs@(NotNil nxs) = case nxs of Sing x -> (x, Nil) Append l r -> ((fst $ cListGet (NotNil l)), (Append (snd $ cListGet (NotNil l)), r)) Which

Getting the head and tail of a custom list type in Haskell

牧云@^-^@ 提交于 2020-01-03 11:46:44
问题 I have a custom list type: data NNList a = Sing a | Append ( NNList a) ( NNList a) deriving (Eq) data CList a = Nil | NotNil ( NNList a) deriving (Eq) I'm trying to implement a function that returns the head and tail of a list: cListGet :: CList a -> Maybe (a, CList a) My attempt: cListGet :: CList a -> Maybe (a, CList a) cListGet Nil = Nothing cListGet xs@(NotNil nxs) = case nxs of Sing x -> (x, Nil) Append l r -> ((fst $ cListGet (NotNil l)), (Append (snd $ cListGet (NotNil l)), r)) Which

Where can I find the OCaml Option module?

ぐ巨炮叔叔 提交于 2020-01-03 09:12:28
问题 I mean this module : Option I can't find it, open Option gives me Error: Unbound module Option and there is no 'option.cma' file Is it in the standard library ? Is it named 'option.cma' ? 回答1: This is not part of the OCaml standard library, no. It looks like it might be part of a former library named Extlib. Extlib, in turn, seems to have become part of OCaml Batteries Included. The Option module is now named BatOption. If you want this module, you should get OCaml Batteries Included. Then

Where can I find the OCaml Option module?

霸气de小男生 提交于 2020-01-03 09:12:27
问题 I mean this module : Option I can't find it, open Option gives me Error: Unbound module Option and there is no 'option.cma' file Is it in the standard library ? Is it named 'option.cma' ? 回答1: This is not part of the OCaml standard library, no. It looks like it might be part of a former library named Extlib. Extlib, in turn, seems to have become part of OCaml Batteries Included. The Option module is now named BatOption. If you want this module, you should get OCaml Batteries Included. Then

What operation does the function perform?

荒凉一梦 提交于 2020-01-03 06:47:05
问题 int function(uint32_t *r, const uint32_t *a, const uint32_t *b, int n) { int i; uint32_t ri, c=0; for (i = 0; i < n; i ++) { ri = a[i] + b[i] + c; c = ((ri < a[i]) || ((ri == a[i]) && c)); r[i] = ri; } return ((int) c); } The C function given below has four arguments: r, a, and b are pointers to arrays of type uint32_t. The integer n specifies the length of these arrays (i.e. all three arrays contain the same number of elements). The return value is of type int. Can anyone could help me to

Unexpected 'sortBy' Behavior with Ramda

倾然丶 夕夏残阳落幕 提交于 2020-01-03 05:13:09
问题 Application of 'sortBy' producing unexpected results. I've gotta be doing something stoopid. This is such a basic operation. const input = [4,3,2,1]; const sort = list => R.sortBy(R.ascend(R.identity))(list); console.log(sort(input)); // [ 4, 3, 2, 1 ] I would expect the output of the 'console.log' invocation to be [ 1, 2, 3, 4 ], but it is not: the output is [ 4, 3, 2, 1 ], same as the input. What am I doing wrong? 回答1: As pointed out by Aadit M Shah in the comments you're not using sortBy

lodash curry does not work on function returned by flow; lodash FP enough for FP?

☆樱花仙子☆ 提交于 2020-01-03 04:35:21
问题 Is the lodash flow function a real compose function, or is it something that looks like one, but is optimized to run fast and sacrifices the flexibility I'd expect? I expected flow to return a function I could curry, but instead it gave back a function that uses Javascript's arguments keyword. So curry can't tell that there are pending arguments, and it just gets invoked immediately. Working intuitively enough: var add = function(x, y) { return x + y }; var exclam = function(x) { return x

Haskell compare all list items

别说谁变了你拦得住时间么 提交于 2020-01-03 04:34:06
问题 Edit: it's hard to describe what I'm trying to do, but here's a try (from the comments): I am building a wordfeud solver, so I have a word, and some letters (both char list). I applied this ( How to find the frequency of characters in a string in Haskell? ) to both lists to get the frequency of all letters. What I'm doing now is iterating though the 'word' char list, and checking if all chars occur sufficiently in the 'letters' char list. I have written a Haskell function that compares two