我如何将Haskell翻译成F#? [英] how do i translate this Haskell to F#?
问题描述
我试图通过翻译一些很久以前我写的Haskell代码来学习F#,但我被卡住了!
I'm trying to learn F# by translating some Haskell code I wrote a very long time ago, but I'm stuck!
percent :: Int -> Int -> Float
percent a b = (fromInt a / fromInt b) * 100
freqs :: String -> [Float]
freqs ws = [percent (count x ws) (lowers ws) | x <- ['a' .. 'z']]
let percent a b = (float a / float b) * 100.
虽然我不喜欢必须拥有。 100美元以后。
although i dont like having to have the . after the 100.
我在 freqs
中执行的操作的名称是什么,以及如何翻译它到F#?
What is the name of the operation I am performing in freqs
, and how do I translate it to F#?
编辑: count
和降低
是 Char - >字符串 - > Int
和字符串 - > Int
分别和我已经翻译过这些。
count
and lowers
are Char -> String -> Int
and String -> Int
respectively, and I have translated these already.
谢谢
推荐答案
这是一个列表理解,在F#中它看起来像下面的最后两行:
This is a list comprehension, and in F# it looks like the last two lines below:
// stub out since dunno implementation
let count (c:char) (s:string) = 4
let lowers (s:string) = 10
// your code
let percent a b = (float a / float b) * 100.
let freq ws = [for x in ['a'..'z'] do
yield percent (count x ws) (lowers ws)]
更一般地说,我认为Haskell列表解析具有以下示例所建议的形式,显示相应的F#。
More generally I think Haskell list comprehensions have the form suggested by the example below, and the corresponding F# is shown.
// Haskell
// [e(x,y) | x <- l1, y <- l2, pred(x,y)]
// F#
[for x in l1 do
for y in l2 do
if pred(x,y) then
yield e(x,y)]
这篇关于我如何将Haskell翻译成F#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!