在Haskell,我能做到
filter pred list
Run Code Online (Sandbox Code Playgroud)
使用list函数pred为true的元素创建新列表.Java API是否有类似的东西java.util.List或其他集合?我无法在API文档中找到任何内容.
我是Python的新手,来自PHP背景......我印象深刻.有没有办法从这个序列中获取数字列表:
i^2, i^2 + i, i^2 + 2i, ..., n
Run Code Online (Sandbox Code Playgroud)
即如果i=2和n=30:
4, 6, 8, ..., 30
好的,这是一个非常简单的序列,但是在功能上做这种事情的更通用的方法是什么?
也许有一些很好的函数式编程方式来做到这一点?在PHP中我想我会做一些函数一些变量和交互,但Python可能能够更优雅地做到这一点?
非常感谢.
我有一个方法,将返回1到100之间的数字
取决于响应(x)是否在100-90之间我想要一个响应,89-85另一个84-72另一个......等等
我用
if x > 90
response a
elsif x > 85
response b
elsif etc...
Run Code Online (Sandbox Code Playgroud)
但这看起来有点乱,是否有更好的重构方法?
非常感谢.
是否有一种优雅的内置(功能样式)方式来选择NJavascript数组中的最后成员?
我在F#中定义一个秒表:
open System.Diagnostics
let UptimeStopwatch = Stopwatch.StartNew()
Run Code Online (Sandbox Code Playgroud)
我每3秒打印一次
printfn "%A" UptimeStopwatch.Elapsed
Run Code Online (Sandbox Code Playgroud)
每当我得到"00:00:00.0003195"或类似的小东西.每次我引用UptimeStopwatch时F#都会调用构造函数吗?如果是这样,我如何绕过这个广告达到预期的效果?这是功能和命令式编程的混乱混合.
我需要在lisp中编写一个带有两个参数的函数 - 无参数函数列表和整数列表.我需要按照第二个列表的顺序评估第一个列表中的函数,即(有趣的'(#'a#b#'c)'(2 0 1))应该评估c,a,b.我试过这样的功能:
(defun z4(funs kols)
(funcall (nth (first kols) funs))
(z4 funs (rest kols))
)
Run Code Online (Sandbox Code Playgroud)
但是在funcall我发现错误
NIL不属于CONS类型.
这是什么意思?通过简单地调用我得到同样的错误
(funcall (first funs))
Run Code Online (Sandbox Code Playgroud)
所以我认为它与从函数列表中获取函数有关.如何从函数列表中评估函数?
我在SML上写这个函数.它应该列出可能的名字变体(我的名字是Victoria,所以V,Vic,Vicky等)并创建{altname1,middle,last},{alt2,middle,last}的记录.
所以这是我的代码:
fun similar_names (substits:, name) =
let
val {first=n1, second=n2, third=n3} = name
fun name_constructor (altnames:string list, acc) =
case altnames of
[] => acc
| a::aa => {first=a, second=n2, third=n3}::acc
in
name_constructor( get_substitutions2(substits, n1),name)
end
Run Code Online (Sandbox Code Playgroud)
get_substitutions2只会给出一个名字的所有可能变体的列表(即:字符串列表),并且它可以工作.
我得到的错误是:
a02.sml:65.2-65.58 Error: operator and operand don't agree [tycon mismatch]
operator domain: string list * {first:string, second:'Z, third:'Y} list
operand: string list * {first:string, second:'Z, third:'Y}
in expression:
name_constructor (get_substitutions2 (substits,n1),name)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它只在记录列表和记录之间进行.你能帮忙吗?
我学习哈斯克尔.我遇到的问题是我无法保存中间计算步骤.感觉无效.如何在函数式编程中使用动态编程?
嗯,我有使用问题semicolon (single and double)和nested if elseOCaml中.
例如
let union u p q =
let rec unionfy id_ary i =
if i < Array.length id_ary then begin
if id_ary.(i) = p then begin
id_ary.(i) <- id_ary.(q);
print_array id_ary 0;
end
unionfy id_ary (i + 1);
end
else print_string "end of union";
in
unionfy u.id_ary 0;;
Run Code Online (Sandbox Code Playgroud)
编译说 line 18, characters 29-95:
Error: This expression is not a function; it cannot be applied
有问题的线是if id_ary.(i) = p then …
假设我有两个.ml文件:A.ml和B.ml.
在A.ml,我有
type my_type = {id_ary : int array; sz_ary : int array};;
Run Code Online (Sandbox Code Playgroud)
在B.ml,我有
let test_my_type {id_ary;_} = id_ary.(0) <- 10;;
Run Code Online (Sandbox Code Playgroud)
然后我像这样编译它们
ocamlc -linkpkg A.ml B.ml -o C
Run Code Online (Sandbox Code Playgroud)
但编译器给出了这样的错误: Error: Unbound record field label id_ary
似乎B无法使用my_type来自的类型A.
我该怎么办?