一旦编译并运行,这将表现为尾调用吗?
let rec f accu = function
| [] -> accu
| h::t -> (h + accu) |> f <| t
Run Code Online (Sandbox Code Playgroud)
也许有一种简单的方法来测试我不知道的行为,但这可能是另一个问题.
我正在写一段来自"Real World Haskell"的代码:
ghc --make ch04/InteractWith.hs
[1 of 1] Compiling Main ( ch04/InteractWith.hs, ch04/InteractWith.o )
ch04/InteractWith.hs:9:5: parse error on input `args'
Run Code Online (Sandbox Code Playgroud)
dan @dbmint~/testHaskell $ cat ch04/InteractWith.hs
import System.Environment (getArgs)
interactWith function inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (function input)
main = mainWith myFunction
where mainWith function = do
args <- getArgs
case args of
[input, output] -> interactWith function input output
_ -> putStrLn "error: exactly two arguments needed"
myFunction = id
Run Code Online (Sandbox Code Playgroud) 考虑以下功能:
let private actionPixel(pixelColour:Color) =
match (pixelColour.A, pixelColour.R, pixelColour.G, pixelColour.B) with
| (0uy, _, _, _) -> transparent
| (alpha, red, green, blue) when red = blue && red = green && red <> 255uy ->
Color.FromArgb(calculateAlpha (int alpha) (int red), 0, 0, 0)
| _ -> pixelColour
Run Code Online (Sandbox Code Playgroud)
我想要做的是替换| (alpha, red, green, blue) when red = blue && red = green && red <> 255uy ->用| (alpha, value, value, value) when value <> 255uy ->.如果我这样做,我会收到'value' …
在试图理解该类型的中缀运算符xfy并且yfx具有相同的优先级和顺序时,我看到只有四种组合.
运用
a = xfy right-associative
b = yfx left-associative
Run Code Online (Sandbox Code Playgroud)
有
aa e.g. 1 xfy 2 xfy 3 e.g. 1 ^ 2 ^ 3
ab e.g. 1 xfy 2 yfx 3
ba e.g. 1 yfx 2 xfy 3
bb e.g. 1 yfx 2 yfx 3 e.g. 1 - 2 - 3
Run Code Online (Sandbox Code Playgroud)
现在对于(xfy xfy)aa,运算符都是正确关联的.
对于(yfx yfx)bb,运营商都是左联盟.
但是对于(xfy yfx)ab ,(xfy)a运算符是右关联的,而(yfx)b运算符是左关联的.如果我理解正确,Prolog将评估为:
使用优先权500和x意义<和y意义 …
我有一个txt文件,其中包含数字和逗号分隔的行,例如.
4324,1dd3,444
4324,1fd3,444
4324,1as3,442
我有一个函数,它接受一个字符串作为参数,我想检查每一行,如果该字符串等于该行的第二个"字",所以第一行将是"1dd3".如果该参数匹配单词我想将行的第三个单词添加到字符串列表中(或者以其他方式保存它以便稍后在代码中使用它).
在C#中,我只是遍历列表并在逗号上使用这样的分割
while(reader.Peek > 0) //while txt file still has lines left to read
{
split the row...
compare it to argument
add to list of strings
}
Run Code Online (Sandbox Code Playgroud)
但我对F#相当新,并且在这里找不到正确的语法和/或方法,任何帮助都将不胜感激.
我正在尝试创建一个函数,将元组与元组数组的值进行比较.我需要这个返回一个布尔值,但是VS2013一直告诉我:"这个表达式应该有'unit'类型,但是类型'bool'"
let compare (i,j,a,(x : (int*int*int) array)) =
for q in 0 .. x.GetLength(0) do
match (i,j,a,x) with
| (i,j,a,x) when (i,j,a) = x.[q] -> true
| _ -> false
Run Code Online (Sandbox Code Playgroud)
还尝试将值作为两个参数给出,但它也不起作用:
let compare (i,j,a) (x : (int*int*int) array) =
for q in 0 .. x.GetLength(0) do
match (i,j,a) with
| x.[q] -> true
| _ -> false
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
我正在寻找一种在F#中定义无参数lambda表达式的方法,就像下面的C#示例一样.
var task = () => {
int x = 3;
DoSomething(x);
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容
let task = fun _ ->
let x = 3
doSomething x
Run Code Online (Sandbox Code Playgroud)
它编译但它给task : ('a -> unit)了我我真正想要的是task : (unit -> unit)
在MSDN文档不谈论这个.我在这里错过了什么?
我刚刚开始使用F#,但我有一些类似于以下内容的代码:
let square x = x*x
let result = square 5.1
let result' = square 12
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会导致以下错误: This expression was expected to have type float but here has type int
这个问题是否有惯用的F#解决方案,还是我的想法被我的C#体验所污染?
好吧,所以我基本上试图将绑定运算符添加到选项类型中,似乎我尝试的所有内容都有一些不明显的警告,阻止我这样做.我怀疑是否与.NET类型系统的限制有关,可能与用户代码中无法实现类型类的原因相同.
无论如何,我尝试过几件事.
首先,我尝试了以下内容
let (>>=) m f = ???
Run Code Online (Sandbox Code Playgroud)
意识到我想根据类型做不同的事情m.F#不允许在函数上重载,但.NET允许在方法上使用它们,所以尝试第二个:
type Mon<'a> =
static member Bind(m : Option<'a>, f : ('a -> Option<'b>)) =
match m with
| None -> None
| Some x -> f x
static member Bind(m : List<'a>, f : ('a -> List<'b>)) =
List.map f m |> List.concat
let (>>=) m f = Mon.Bind(m, f)
Run Code Online (Sandbox Code Playgroud)
没有骰子.无法根据以前给定的类型信息选择唯一的重载.添加类型注释.
我已经尝试使运算符内联,但它仍然给出相同的错误.
然后我想我可以使>>=运算符成为一个类型的成员.我很确定这会有效,但我认为我不能在现有类型上进行破解.您可以扩展现有类型,type Option<'a> with但不能将运算符作为扩展.
这是我对此代码的最后一次尝试:
type Option<'a> with
static member (>>=) (m : …Run Code Online (Sandbox Code Playgroud) 给定以下数据类型定义:
data FormTree = Empty | Node FormTree FormTree deriving Show
Run Code Online (Sandbox Code Playgroud)
我想编写一个函数,它生成一个无限列表,其中包含按长度排序的所有可能的树,例如节点的数量.
下面的代码几乎可以满足我的需要,但它只是通过每次插入额外的节点来降低右侧的树,但我需要它在两边之间交替.
allPossibleTrees :: [FormTree]
allPossibleTrees = Empty : [Node x y | x <- recursive, y <- recursive]
where recursive = allPossibleTrees
Run Code Online (Sandbox Code Playgroud)
执行
take 5 allPossibleTrees
Run Code Online (Sandbox Code Playgroud)
得到:
[Empty,Node Empty Empty,Node Empty (Node Empty Empty),Node Empty (Node Empty (Nodes Empty Empty)),Node Empty (Node Empty (Node Empty (Node Empty Empty)))]
Run Code Online (Sandbox Code Playgroud)
但它应该是这样的:
[Empty,Node Empty Empty,Node (Node Empty Empty) Empty,Node Empty (Node Empty Empty),Node (Node Empty Empty) (Node Empty Empty)]
Run Code Online (Sandbox Code Playgroud) f# ×7
c#-to-f# ×2
guard-clause ×2
haskell ×2
inline ×2
unit-type ×2
binary-tree ×1
c# ×1
catalan ×1
iso-prolog ×1
operators ×1
optional ×1
tail-call ×1
tuples ×1
types ×1