functional-programming

Turn a list with common items in to a list of ordered pairs

回眸只為那壹抹淺笑 提交于 2019-12-24 10:55:42
问题 I am entirely new to functional programming; this is a homework assignment for SML. I have a list of integers and I am trying to get a list of ordered pairs where the second entry of the pair is the number of times the first entry appeared in the initial list. For example: [2,3,3,5] => [(2,1),(3,2),(5,1)] I'm not hoping for somebody to implement this but rather give me an idea of what sort of higher-order function I am looking for, and/or a pointer in the right direction. Again, totally new

Swift 4.0 Difference between implementing custom operator inside a type as a type method and in global scope as a global function

两盒软妹~` 提交于 2019-12-24 10:49:17
问题 Given a Parser type as following: public struct Parser<Result> { internal let parse: (String) -> (Result, String)? public func run(_ string: String) -> (Result, String)? { guard let (result, remainder) = parse(string) else { return nil } return (result, remainder) } public func map<T>(_ transform: @escaping (Result) -> T ) -> Parser<T> { return Parser<T> { input in guard let (result, remainder) = self.run(input) else { return nil } return (transform(result), remainder) } } public func

Return a pair - syntax error

。_饼干妹妹 提交于 2019-12-24 10:47:33
问题 I'm using pl in racket: https://pl.barzilay.org/ The download can be found here: http://pl.barzilay.org/pl.plt ( : f1 : -> (Pairof Symbol String)) (define (f1) (cons 'wwww "aaa")) Error: Type Checker: Polymorphic function `cons' could not be applied to arguments: Argument 1: Expected: a Given: 'wwww Argument 2: Expected: (Listof a) Given: String Result type: (Listof a) Expected result: (Pairof Symbol String) in: (cons (quote wwww) "aaa") What I did wrong and how can I fix it? 回答1: The #lang

“Updating” a map/data structure in consecutive function calls in Clojure

爱⌒轻易说出口 提交于 2019-12-24 10:46:37
问题 I want to "update" a map through multiple calls of functions, in Clojure. The idea is expressed as below: (defn foo1 [a-map] (assoc a-map :key1 "value1")) (defn foo2 [a-map] (assoc a-map :key2 "value2")) (defn foo3 [a-map] (assoc a-map :key3 "value3")) (defn -main [] (let [a-map {}] (do (foo1 a-map) (foo2 a-map) (foo3 a-map) a-map))) Apparently this piece of code is wrong because the a-map is not updated outside the scope of subroutines. It's written like this simply because it's clearer as

SCHEME - Writing my own append produces a weird result

半城伤御伤魂 提交于 2019-12-24 10:44:58
问题 I want to write my own append , for appending an element to an existing list . I've written the following : (define (appendElem llist elem) (if (null? llist) elem (cons (car llist) (appendElem (cdr llist) elem)))) But when I do this : (appendElem (list 1 2 30) 11) I get : (1 2 30 . 11) So the question is , why (1 2 30 . 11) and not (1 2 30 11) ? Thanks EDIT: Fixed : (define (appendElem llist elem) (if (null? llist) (list elem) (cons (car llist) (appendElem (cdr llist) elem)))) 回答1: Think

How does outermost evaluation work on an application of a curried function?

只愿长相守 提交于 2019-12-24 08:58:34
问题 mult is defined as a curried function: mult :: Int -> Int -> Int mult x = \y -> x * y In mult (1+2) (2+3) , what are the redex's. and are they mult(1+2) , 1+2 and 2+3 ? What is the outermost redex, and is it 2+3 ? Innermost evaluation works on the expression as following, according to Programming in Haskell by Hutton: mult (1+2) (2+3) = { applying the first + } mult 3 (2+3) = { applying mult } (\y -> 3 * y) (2+3) = { applying + } (\y -> 3 * y) 5 = { applying the lambda } 3 * 5 = { applying *

Converting Stream into IntStream

做~自己de王妃 提交于 2019-12-24 08:44:23
问题 I am setting the id for each list item where primary address for two list are equal. Server POJO public class Server { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") public Integer id; @OneToMany (mappedBy = "server",cascade = CascadeType.ALL, orphanRemoval = true) private List<IPAddress> ipaddresses; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setIpaddresses(List<IPAddress> ipaddresses) { this.ipaddresses =

Ramda JS best way to get the tightest geographic bound

↘锁芯ラ 提交于 2019-12-24 08:39:45
问题 I am attempting to use the Google Places API in order to get the place name of a location I am in. The returned data structure has the following types: descriptor1: 'street number' | 'neighborhood' | 'postcode' | 'route' | 'locality' | 'postal_town' | 'administrative_area_level_2' | 'administrative_area_level_1' | 'country' places: [ { address_components: [{ long_name: 'string', short_name: 'string', types: { 0: descriptor1, 1?: descriptor2 } }], other_fields not relevant here } ] There is no

Simplest way to solve a maze without mutability

夙愿已清 提交于 2019-12-24 08:34:53
问题 After learning some Scala and the benefits of FP, I am reimplementing some of my previous CS assignments to better understand FP. However, I got to one assignment that seems impractical to implement with FP (or at least trivially translate). When solving a simple 2D maze it is necessary to remember which nodes have been visited. However, without shared state, how can each recursive call know what nodes the other recursive calls have examined? I could pass the maze as a parameter to each

Is my understanding of a reducible expression i.e. redex correct?

浪尽此生 提交于 2019-12-24 08:25:59
问题 Programming in Haskell by Hutton says: An expression that has the form of a function applied to one or more arguments that can be ‘reduced’ by performing the application is called a reducible expression, or redex for short. Is a reducible expression i.e. redex exactly a function application where the function is not the result of another function application, equivalently, a function application where the function is either a function name or a lambda expression? Is either of the above two