functional-programming

Create random data from custom type

六月ゝ 毕业季﹏ 提交于 2019-12-12 13:53:35
问题 I have the following custom type defined data Tree = Empty | Node Tree Tree I want to create random Tree s with a given number of nodes n that I can then pass to another function which calculates the depth of the tree depth :: Tree -> Int depth Empty = 0 depth Node t1 t2 = (maximum [depth t1, depth t2]) + 1 Which is the easiest way to achieve this? EDIT: I have tried with an approach similar to that of Alec in an answer below, which returns a random IO Tree . However, there are several other

why in Scala a function type needs to be passed in separate group of arguments into a function

允我心安 提交于 2019-12-12 13:47:00
问题 I am new to scala , I have written same code in 2 ways . But i am bit confused between 2 ways. In Second way argument type of f are derived automatically but in type1 scala compiler is not able to do the same . I just want to understand what is the thought behind this . Type1: Gives compilation error def rightFold[A,B](xs:List[A],z:B,f:(A,B) => B ): B = xs match { case Nil => z case Cons(x,xs) => f(x,rightFold(xs,z,f)) } def sum1(l:List[Int]) = rightFold(l,0.0,_ + _) Type2 : Works fine def

product of two functions

孤街醉人 提交于 2019-12-12 13:18:36
问题 I have two functions, f and g . Both have the same signature: (x) . I want to create a new function, z , with the same signature: def z(x): return f(x) * g(x) except that I'd like to be able to write z = f * g instead of the above code. Is it possible? 回答1: The funny thing is that it is quite possible. I made a project some days ago to do things like that. Here it is: FuncBuilder By now you can only define variables, but you can use my metaclass with the help of some other functions to build

ML function currying

情到浓时终转凉″ 提交于 2019-12-12 13:06:36
问题 Could someone please explain the concept of currying to me. I am primarily learning it because we are learning ML in my 'modern programming language' class for a functional language introduction. In particular you can use this example: -fun g a = fn b => a+b; val g = fn: int -> int -> int -g 2 3; val it = 5 : int I'm confused how these parameters are passed or how to even think about it in the first place. Thank you for any help. 回答1: In this case, you make the currying explicit, so it should

How to write a recursive function that takes a list and return the same list without vowels?

假如想象 提交于 2019-12-12 12:43:54
问题 I am supposed to write a recursive function that takes a list of strings or a list of lists of strings and return the list without vowels, if found. Here is my attempt to solve it: def noVow(seq): keys = ['a','i','e','o','u','u'] if not seq or not isinstance(seq, list) : return else: if seq[0] in keys: del seq[0] return (noVow(seq[0:])) else: return (noVow(seq[1:])) li = ["b", "c", "d","a"] print (noVow(li)) I am aware that the bug lies in my base case however I can't come up with the right

Can I annotate the complete type of a `fun` declaration?

ⅰ亾dé卋堺 提交于 2019-12-12 12:22:36
问题 In a learning environment, what are my options to provide type signatures for functions? Standard ML doesn't have top-level type signatures like Haskell. Here are the alternatives I have considered: Module signatures, which require either a separate signature file, or the type signature being defined in a separate block inside the same file as the module itself. This requires the use of modules, and in any production system that would be a sane choice. Modules may seem a little verbose in a

Breaking an integer up into chunks, functionally

橙三吉。 提交于 2019-12-12 12:18:48
问题 Consider the problem of decomposing milliseconds into readable time units. Imagine you had a function that did that > breakupMillis(100000000) Array [ 0, 40, 46, 3, 1 ] meaning that 100 million milliseconds is 1 day, 3 hours, 46 minutes, and 40 seconds, exactly. The function could be generalized by accepting an array of moduli, like this > breakup(100000000, [1000, 60, 60, 24]) Array [ 0, 40, 46, 3, 1 ] That function could be used (hypothetically) for other things: > breakup(1000, [8, 8, 8])

What is the difference between mutable values and immutable value redefinition?

痴心易碎 提交于 2019-12-12 11:55:10
问题 I have read that values in F# are immutable. However, I have also come across the concept of redefining value definitions, which shadow the previous ones. How is this different from a mutable value ? I ask this not just as a theoretical construct, but also if there is any advice on when to use mutable values and when to redefine expressions instead; or if someone can point out that the latter is not idiomatic f#. Basic example of redefinition: let a = 1;; a;; //1 let a = 2;; a;; //2 Update 1:

F# multi-condition if/else versus matching

这一生的挚爱 提交于 2019-12-12 11:26:31
问题 I'm new to F# and have been implementing simple algorithms to learn the language constructs. I implemented the bisection method using if/else and then wanted to learn how to do it using matching. if fc = 0.0 then printfn "%i/%i - f(%f) = %f, f(%f) = %f, f(%f) = %f" new_count n a fa b fb c fc else if ((b - a) * 0.5) < eps then printfn "%i/%i - f(%f) = %f, f(%f) = %f, f(%f) = %f" new_count n a fa b fb c fc else if new_count = n then printfn "%i/%i - f(%f) = %f, f(%f) = %f, f(%f) = %f" new_count

Difference between higher order and curried functions

你说的曾经没有我的故事 提交于 2019-12-12 10:54:11
问题 I'm reading a book, Functional Programming Using F#, which says (page 33), in the section Declaration of higher-order functions We have seen higher-order built-in functions like (+) and (<<) and at the end of the section Higher-order functions may alternatively be defined by supplying the arguments as follows in the let-declaration: let weight ro s = ro * s ** 3.0;; However there were some helpful comments at the bottom of a question that I asked earlier today (which was originally titled