我需要编写一个函数,它接受一个字符串列表并找到列表中最大的字符串.问题是它需要使用List.foldl迭代列表,并且不能使用递归调用,除了List,foldl的库函数中的那些调用.
我写
fun longest_string1(xs)=
case xs of
[] => ""
| x::xs' => List.foldl((fn (s,x) => if String.size s > String.size x then s else x) "" x,)
Run Code Online (Sandbox Code Playgroud)
我的解释如下:
-s in xs,如果xs为空则返回一个空字符串 -
另外xs调用的第一项List.foldl
-List.foldl传入一个匿名函数来检查s的长度,这应该是表示针对列表的head项的累加器.
- 将初始累加器设置为空字符串,将初始比较值设置为高阶函数传入的初始列表的头部
但是,它不进行类型检查.
我认为我的问题在于理解List.foldl函数本身以及它如何读取其参数.有人可以提供一些澄清吗?
我正在尝试在Racket中编写一个模块元语言mylang,它接受第二种语言传递修改后的正文,这样:
(module foo mylang typed/racket body)
Run Code Online (Sandbox Code Playgroud)
相当于:
(module foo typed/racket transformed-body)
Run Code Online (Sandbox Code Playgroud)
当然,typed/racket部件可以用任何其他模块语言替换.
我尝试了一个简单的版本,让身体保持不变.它在命令行上运行正常,但在DrRacket中运行时出现以下错误:
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:479:30: require: namespace mismatch;
reference to a module that is not available
reference phase: 1
referenced module: "/usr/share/racket/pkgs/typed-racket-lib/typed-racket/env/env-req.rkt"
referenced phase level: 0 in: add-mod!
Run Code Online (Sandbox Code Playgroud)
这是整个代码:
#lang racket
(module mylang racket
(provide (rename-out [-#%module-begin #%module-begin]))
(require (for-syntax syntax/strip-context))
(define-syntax (-#%module-begin stx)
(syntax-case stx ()
[(_ lng . rest)
(let ([lng-sym (syntax-e #'lng)])
(namespace-require `(for-meta -1 ,lng-sym))
(with-syntax ([mb (namespace-symbol->identifier '#%module-begin)])
#`(mb . …Run Code Online (Sandbox Code Playgroud) 我一直致力于规范/ kitchennsink的元语言,可以编译成PHP一段时间了.现在我想开始构建这个东西.在我使用PHP_Lexergenerator和PHP_Parsergenerator实现微型DSL之前,它们已经运行良好,但我之前从未构建任何这样的规模.非常感谢您提供的任何反馈/建议/经验!
我在http://pastebin.com/613mFGsB粘贴了规范.
TL;DR:Metal 似乎没有检测到我的顶点着色器返回的内容
我有这两个用 MSL 编写的函数:
vertex float4 base_image_rect(constant float4 *pos [[buffer(0)]],
uint vid [[vertex_id]]) {
return pos[vid];
}
fragment float4 fragment_image_display(float4 vPos [[stage_in]],
texture2d<float, access::sample> imageToRender [[texture(0)]],
sampler imageSampler [[sampler(0)]]) {
return imageToRender.sample(imageSampler, float2(vPos.x, vPos.y));
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用这些代码创建渲染管道状态时:
// Make image display render pipeline state
let imageDisplayStateDescriptor = MTLRenderPipelineDescriptor()
imageDisplayStateDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
imageDisplayStateDescriptor.vertexFunction = library.makeFunction(name: "base_image_rect")
imageDisplayStateDescriptor.fragmentFunction = library.makeFunction(name: "fragment_image_display")
displayImagePipelineState = try! device.makeRenderPipelineState(descriptor: imageDisplayStateDescriptor)
Run Code Online (Sandbox Code Playgroud)
创建管道状态时出现错误:
致命错误:“尝试!” 表达意外加一个错误:错误域= CompilerError代码= 1“链接失败:片段VPOS输入未 在发现的顶点着色器输出” [...]
我检查并重新检查了代码,无法理解出了什么问题。
有任何想法吗?谢谢!