functional-programming

How to keep elements in list through out the program in SML?

白昼怎懂夜的黑 提交于 2020-01-25 21:34:13
问题 Suppose I have to update a list with each call to a function, such that, the previous element of the list are preserved. Here is my attempt: local val all_list = []; in fun insert (x:int) : string = int2string (list_len( ((all_list@[x])) ) ) end; The problem is that each time I call to insert, I get the output "1", which indicates that the list is initiated to [] again. However I was expecting output of "1" for the first call to insert, and "2" for the second call,...etc. I am not able to

Scala : zipWith[A,B,C](f: Function2[A,B,C], l1 :List[A], l2 :List[B]) : List[C] Method

佐手、 提交于 2020-01-25 21:31:23
问题 So i need to implement a function that takes two lists and a function. The function then uses the elements of the two lists and applies the function on the elements and saves them into a list by using map and/or fold and the functions from the list class. Example: • zipWith((x: Int, y: Int) => x + y, List(1, 2, 3), List(4, 5, 6)) → List(5, 7, 9) • zipWith((x:Int,y:Int) => x, List(1,2,3), List(4,5,6)) → List(1, 2, 3) I do not know how to use the passed function and apply it on the two lists.

Tail recursion in SML does not present any output

元气小坏坏 提交于 2020-01-25 07:31:06
问题 Following my previous post here , I tried to do what was suggested and convert the code into a Tail-recursion method with let . The original code - which does not work (due to using val inside if condition) : fun func() = val decimal = 0 (* the final result *) val multiple = 0 (* keeps track of multiples, eg. In XXV, X would be a multiple *) val current = 0 (* the digit currently being processed *) val top = 0 (* value of the last element in the list *) val last_add = 0 (* the last digit that

Can't open Llvm in ocaml

别等时光非礼了梦想. 提交于 2020-01-25 04:55:08
问题 I'm trying to use llvm binding in ocaml, in my file test.ml, I have one line of code: open Llvm When I run the command ocamlbuild -use-ocamlfind test.byte -package llvm I get this result: + ocamlfind ocamldep -package llvm -modules test.ml > test.ml.depends ocamlfind: Package `llvm' not found Command exited with code 2. Compilation unsuccessful after building 1 target (0 cached) in 00:00:00. What did I do wrong in this? Thanks. BTW, the _tag file contains: "src": traverse <src/{lexer,parser}

What are practical examples of the higher-order functions foldl and foldr?

心不动则不痛 提交于 2020-01-25 03:43:05
问题 The typical academic example is to sum a list. Are there real world examples of the use of fold that will shed light on its utility ? 回答1: fold is perhaps the most fundamental operation on sequences. Asking for its utility is like asking for the utility of a for loop in an imperative language. Given a list (or array, or tree, or ..), a starting value, and a function, the fold operator reduces the list to a single result. It is also the natural catamorphism (destructor) for lists. Any

More functional way to do this?

会有一股神秘感。 提交于 2020-01-24 21:14:42
问题 This post of mine discusses Thomson's paradox, and simulates it in Clojure. The state function returns the state of the lamp at time = t. (defn thomsons-lamp [] (iterate (fn [[onoff dur]] [(not onoff) (/ dur 2)]) [true 1])) (defn state [t] (let [t-vals (map second (thomsons-lamp))] (loop [i 1] (if (<= t (apply + (take i t-vals))) ((comp first last) (take i (thomsons-lamp))) (recur (inc i)))))) How do I define a cleaner state function (preferably without loop/recur)? 回答1: The only sins here

Node - unexpected identifier

最后都变了- 提交于 2020-01-24 07:53:26
问题 I'm trying to play around with Node and some ES6/functional stuff. Here is two files. dog.js const dog = () => { return { test: (arg) => console.log("dog say: " + arg) } } export default dog; 1.js import dog from './dog'; const d = dog() d.test('111'); Node version - 10.4.0 (Node settings are fine) When I'm running node 1.js - getting error Unexpected identifier , pointing on dog. What is wrong here? P.S. 1.js was updated for proper usage of imported function, but even after that I'm still

Node - unexpected identifier

夙愿已清 提交于 2020-01-24 07:53:05
问题 I'm trying to play around with Node and some ES6/functional stuff. Here is two files. dog.js const dog = () => { return { test: (arg) => console.log("dog say: " + arg) } } export default dog; 1.js import dog from './dog'; const d = dog() d.test('111'); Node version - 10.4.0 (Node settings are fine) When I'm running node 1.js - getting error Unexpected identifier , pointing on dog. What is wrong here? P.S. 1.js was updated for proper usage of imported function, but even after that I'm still

In scala, how can I use pattern match to match a list with specified length?

这一生的挚爱 提交于 2020-01-23 16:19:27
问题 My codes looks like this: 1::2::Nil match { case 1::ts::Nil => "Starts with 1. More than one element" case 1::Nil => "Starts with 1. Only one element" } I tried to use 1::ts::Nil to match the List who starts with 1 and whose length is greater than 1. It workes well for 2-element list, however, this pattern doesn't work for 3-element list , for example: 1::2::3::Nil match { case 1::ts::Nil => "Starts with 1. More than one element" case 1::Nil => "Starts with 1. Only one element" } This won't

Play Framework Form “fold” method naming rationale

半城伤御伤魂 提交于 2020-01-23 08:16:07
问题 Play Framework's (2.x) Form class has a method called fold who's usage is indicated as: anyForm.bindFromRequest().fold( f => redisplayForm(f), t => handleValidFormSubmission(t) ) Essentially, the first function parameter is what gets executed on binding failure, and the 2nd on binding success. To me it seems similar to the 'success' and 'error' callbacks of jquery's ajax function. My question is why did the Play developers call the method "fold"? As a disclaimer I am new to Scala, but I am