如何检查功能参数的类型?例如,
def hello(x):
# check if x is tuple
# check if x is dictionary.
...
Run Code Online (Sandbox Code Playgroud)
isinstance(x, type(()))
isinstance(x, type({}))
Run Code Online (Sandbox Code Playgroud) 从这个例子这篇文章有一个例子
open System.IO
let lines =
File.ReadAllLines("tclscript.do")
|> Seq.map (fun line ->
let newLine = line.Replace("{", "{{").Replace("}", "}}")
newLine )
File.WriteAllLines("tclscript.txt", lines)
Run Code Online (Sandbox Code Playgroud)
编译时出错.
error FS0001: This expression was expected to have type
string []
but here has type
seq<string>
Run Code Online (Sandbox Code Playgroud)
如何将seq转换为string []以删除此错误消息?
例如,我有一个字符串数组行
lines = [| "hello"; "world" |]
Run Code Online (Sandbox Code Playgroud)
我想创建一个字符串行,将行中的元素连接起来,前面加上"code ="字符串.例如,我需要code="helloworld"从lines数组中获取字符串.
我可以用这段代码得到连接的字符串
let concatenatedLine = lines |> String.concat ""
Run Code Online (Sandbox Code Playgroud)
我测试了这段代码,在前面添加了"code ="字符串,但是我收到了error FS0001: The type 'string' is not compatible with the type 'seq<string>'错误.
let concatenatedLine = "code=" + lines |> String.concat ""
Run Code Online (Sandbox Code Playgroud)
这有什么问题?
我在F#/ WPF编程上找到了一些好的帖子,但它似乎没有教会编译/构建代码以便用F#/ WPF制作二进制文件的方法.
我用什么命令/工具将这段代码构建/编译成执行二进制文件?刚刚运行fsc或fsi似乎不起作用.
#light
#I @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0"
#r @"WindowsBase.dll"
#r @"PresentationCore.dll"
#r @"PresentationFramework.dll"
open System
open System.Windows
(* From Chap 1 - SayHello.cs *)
let win = new Window()
win.Title <- "Say Hello"
win.Show()
#if COMPILED
[<STAThread()>]
do
let app = new Application() in
app.Run() |> ignore
#endif
Run Code Online (Sandbox Code Playgroud) 我有一个eclipse插件代码来操作项目/工作区中的类(smcho.Hello).我可以创建一个CompilationUnit并对其进行一些修改,但我需要将结果保存在不同的文件中以检查两个版本之间的差异.
这是我获取CompilationUnit的代码.
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Hello");
project.open(null);
IJavaProject javaProject = JavaCore.create(project);
IType lwType = javaProject.findType("smcho.Hello");
org.eclipse.jdt.core.ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit();
final ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(lwCompilationUnit);
parser.setResolveBindings(true); // we need bindings later on
CompilationUnit unit = (CompilationUnit) parser.createAST(null /* IProgressMonitor */);
// modify the unit AST node
Run Code Online (Sandbox Code Playgroud)
如何将此修改后的单元保存到新文件中?
java eclipse eclipse-plugin abstract-syntax-tree eclipse-jdt
假设我有两个相同但不同顺序的lisp列表:'(A B C)和' (C B A).如何检查它们是否相同(在元素相同的意义上)?
CL-USER> (equal '(a b c) '(c b a))
NIL
Run Code Online (Sandbox Code Playgroud) 我可以在LISP中使用关键字参数
(member 'a '(a b c) :test #'eq)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用apply来调用成员方法时
(apply #'member 'a '(a b c) :test #'eq)
Run Code Online (Sandbox Code Playgroud)
我有如下错误信息:
MEMBER: keyword arguments in (:TEST) should occur pairwise
[Condition of type SYSTEM::SIMPLE-PROGRAM-ERROR]
Run Code Online (Sandbox Code Playgroud)
解决方案是
(apply #'member 'a '(a b c) '(:test eq))
Run Code Online (Sandbox Code Playgroud)
而没有关键字参数
(apply #'member 'a '((a b c)))
Run Code Online (Sandbox Code Playgroud)
这背后的逻辑是什么?为什么'(:test#'eq)会引发错误?
这就是我问这个问题的原因.我有来自ANSI Common Lispbook第103页的代码.
(defun our-adjoin (obj lst &rest args)
(if (apply #'member obj lst args)
lst
(cons obj lst)))
Run Code Online (Sandbox Code Playgroud)
当我尝试(our-adjoin 'a '(a b c))它时返回结果(A B C),但是my-adjoin不能被翻译为(apply #'member …
我需要根据元素的长度对列表进行排序,然后根据内容进行排序.例如,对于输入[[1,2,3,4],[1,2,3],[2,3,4]],我需要得到[[1,2,3,4],[2,3,4],[1,2,3]]:[1,2,3,4]具有最大元素,然后[2,3,4]在其第一个元素中大于[1,2,3].对于输入[[2,3,5],[1,2,3],[2,3,4]],[[2,3,5],[2,3,4],[1,2,3]]当元素的长度相同时,应通过逐元素比较返回.
我可以通过元素的长度轻松地对列表进行排序,但是我怎么能在那之后求助呢?
>>> a = [[1,2,3,4],[1,2,3],[2,3,4]]
>>> sorted(a, key=len, reverse=True)
[[1, 2, 3, 4], [1, 2, 3], [2, 3, 4]]
Run Code Online (Sandbox Code Playgroud) 从枚举Scala文档中,我有这个例子.
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
Run Code Online (Sandbox Code Playgroud)
然而,似乎它没有正常工作type WeekDay = Value.这是为了什么?什么时候可以跳过这个?
我需要设置一个Scala Map,它从String映射到一个对象(字符串,整数,浮点数)
我试过这段代码:
val m = Map[String, Object]("A"->10, "B"->20.5)
Run Code Online (Sandbox Code Playgroud)
得到类型不匹配错误.

什么可以解决这个问题?
f# ×3
types ×3
lisp ×2
python ×2
scala ×2
string ×2
apply ×1
common-lisp ×1
comparison ×1
eclipse ×1
eclipse-jdt ×1
enumeration ×1
java ×1
keyword ×1
map ×1
sorting ×1
wpf ×1