functional-programming

Remove a substring/ string pattern of a string in Erlang

笑着哭i 提交于 2020-01-05 10:02:29
问题 I have an xml string like S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/></B>". I want to remove the end tag </B> S2 = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/>" How can I achieve this? 回答1: If you only want to remove the specific string literal </B> then getting a sublist will do the trick: S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/></B>", lists:sublist(S, 1, length(S) - 4). %%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"

Standard ML: Operator and Operand Don't Agree (Circularity)

放肆的年华 提交于 2020-01-05 08:29:26
问题 I'm trying to write a function in SML to flip alternate elements of a list. Here's my function: fun flipAlternate(nil) = nil | flipAlternate([x]) = x | flipAlternate(x::y::xs) = y::x::flipAlternate(xs); When I go to use my file (Ullman.sml) in the interactive interpreter, I get this compilation error: - use "Ullman.sml"; [opening Ullman.sml] Ullman.sml:5.31-5.54 Error: operator and operand don't agree [circularity] operator domain: 'Z list * 'Z list list operand: 'Z list * 'Z list in

Standard ML: Operator and Operand Don't Agree (Circularity)

大兔子大兔子 提交于 2020-01-05 08:29:08
问题 I'm trying to write a function in SML to flip alternate elements of a list. Here's my function: fun flipAlternate(nil) = nil | flipAlternate([x]) = x | flipAlternate(x::y::xs) = y::x::flipAlternate(xs); When I go to use my file (Ullman.sml) in the interactive interpreter, I get this compilation error: - use "Ullman.sml"; [opening Ullman.sml] Ullman.sml:5.31-5.54 Error: operator and operand don't agree [circularity] operator domain: 'Z list * 'Z list list operand: 'Z list * 'Z list in

Partially applied functions in Scala

人盡茶涼 提交于 2020-01-05 07:52:23
问题 Wondering if you can comment on why following two scenarios behave differently: The following works: var la= List(12, 13 , 14 ,15); var func = (x:Int) => println(x) la.foreach(func) // 1 la.foreach(func(_)) // 2 But the following does not: var la= List(12, 13 , 14 ,15); var func1 = (x:Int) => { for (i <- 0 to x) yield i*2 } mkString la.foreach(println(func1)) // similar to 1 above la.foreach(println(func1(_))) // similar to 2 above error: type mismatch; found : Unit required: Int => ? la

Partition a list into equivalence classes

老子叫甜甜 提交于 2020-01-05 06:50:52
问题 I am trying to write a function in SML which when given a list of general elements, reorders its elements into equivalent classes and returns a list of these classes (type "a list list). Leave the elements in the classes in the same order as in the original list. A given function defines the equivalence of the elements and it returns true if the elements are equivalent or false otherwise. I cannot seem to get a grip on the solution. fun sample x y = x = y Required type: fn : (''a -> ''a ->

Generate all permutations of a list including diferent sizes and repeated elements

戏子无情 提交于 2020-01-05 05:10:20
问题 I wanted to create the function genAllSize ::[a] -> [[a]] , that receives a list l and generates all the lists sorted by size that can be built with the elements of the list l ; i.e. > genAllSize [2,4,8] [[],[2],[4],[8],[2,2],[4,2],[8,2],[2,4],[4,4],[8,4],[2,8],[4,8],[8,8],[2,2,2],[4,2,2],[8,2,2], ... How would you do it? I came up with a solution using permutations from Data.List but I do not want to use it. 回答1: Given an input list xs , select a prefix of that in a non deterministic way For

REVERSE function in LISP

佐手、 提交于 2020-01-05 04:55:02
问题 Could anybody explain in detail how the following pure LISP function works: (DEFINE (REVERSE (LAMBDA (L) (REV NIL L)))) (DEFINE (REV (LAMBDA (OUT IN) (COND ((NULL IN) OUT) (T (REV (CONS (CAR IN) OUT) (CDR IN)))))) The function should reverse the order of the elements in a list, that's clear, but I am still unable to understand how it works. * EDIT * okay i believe i figured it out. The REVERSE function is called with a list as argument, and calls a REV function with NIL and that list L as

Spock unit testing assert log calls and see output

女生的网名这么多〃 提交于 2020-01-05 03:06:19
问题 I am using spock to test Java Spring Boot code. It gets a logback logger over the lombok @Slf4j annotation. Dummy class with log call import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Slf4j @Component public class Clazz { public void method() { // ... code log.warn("message", new RuntimeException()); } } The Spock Spec import groovy.util.logging.Slf4j import org.junit.Rule import org.slf4j.Logger import spock.lang.Specification @Slf4j class LogSpec extends

Join elem with next one in a functional style

删除回忆录丶 提交于 2020-01-04 14:05:12
问题 I'm trying to find a way to "join"/"groupby" 2 elements in a list as following : List("a","b","c","d") -> List("ab","bc","cd") With a functional style. Would someone know how to do this? Need I use reducer, fold, scan, other higher-order function? 回答1: Sliding creates subcollections with sliding window, then you just need to map this sublists to strings: List("a","b","c","d").sliding(2,1).map{case List(a,b) => a+b} 回答2: Try val xs = List("a","b","c","d") (xs, xs.tail).zipped.map(_ ++ _) //

Built in f# operator to compose functions with the same input but different outputs?

心不动则不痛 提交于 2020-01-04 11:09:29
问题 I understand the << compose operator takes two functions that both take in and return the same type. e.g. (lhs:'a -> 'a) -> (rhs:'a -> 'a) -> 'a I often find myself wanting something like (lhs:'a -> 'b) -> (rhs:'c -> 'b) -> 'b in cases where I'm interested in side affects and not the return value 'b is probably the unit type. This is only when I have two lines in succession where I'm persisting something to a database. Is there a built in function or idiomatic F# way of doing this without