




本帖最后由 Richeir 于 2012-8-1 13:40 编辑
既然各位大神迟迟不发Ruby的教程, 我就先把我这2天学到的东西写出来.首先申明,我不会Ruby ,就在网上看了一点点,连if 和 for都没看到.IronRuby是微软的Ruby实现,Iron的意思是” Implementation running on .NET微软的.NET的CLR和Mono(非官方的,PSV就是用这个)都支持IronRuby.这次帖子主要讲的是C#和IronRuby的混合编程.首先肯定是说明如何用C#执行IronRuby脚本.然后分为2块 : C#调用IronRuby里面的类或函数.IronRuby调用C#里面的类或函数.安装啥的我就不说了,自己去谷歌吧.关键就是要安装完后的几个程序集.新建一个命令行工程,引用这3个程序集,和这3个(在.net里面).记得写命名空间.首先来看怎么执行Ruby脚本在项目里面新建一个Ruby文件(“Demo.rb”),代码如下[mw_shl_code=ruby,true]puts “Hello,IronRuby”[/mw_shl_code]
会到C#在Main函数里面写上[mw_shl_code=csharp,true] ScriptEngine e = Ruby.CreateEngine();stringcode;using(var fs = File.Open("Demo.rb", FileMode.Open)){StreamReadersr = new StreamReader(fs);code = sr.ReadToEnd();}e.Execute(code);Console.ReadKey();//用来暂停,好看结果[/mw_shl_code]执行下 就会看到命令行输出这就是用C#执行IronRuby.接下来是IronRuby调用C#的类,函数在C#里面定义函数[mw_shl_code=csharp,true]static public void CSharp_Puts(stringst){Console.WriteLine(st);}[/mw_shl_code]Ruby代码如下[mw_shl_code=ruby,true]require 'System'require 'ConsoleIronRuby'ConsoleIronRuby::Program.CSharp_Puts("Hello,CSharp!")System:: Console.WriteLine("Hello,IronRuby");[/mw_shl_code]require的作用是引用程序集,这是IronRuby的优势之一,直接引用.net程序集ConsoleIronRuby是我们工程的名字再来看C#里面如何调用IronRuby的函数先写Ruby代码[mw_shl_code=ruby,true]class Exampledef sayputs "PrintFrom Ruby"endend[/mw_shl_code]C#:[mw_shl_code=csharp,true]ScriptEngine e = Ruby.CreateEngine();string code;using (var fs = File.Open("Demo.rb", FileMode.Open)){StreamReader sr = new StreamReader(fs);code = sr.ReadToEnd();}e.Execute(code);dynamic ruby = e.Runtime.Globals;dynamic example = ruby.Example.@new();example.say();Console.ReadKey();[/mw_shl_code]结果如下:Dynamic 关键字是运行时解析类型,专门给脚本语言用的@new()是调用构造函数,@的目的是避开C#关键字.上面就把几个基本的运行模式讲完了.下次再说如何在2个语言之间传值,最后我会给出一个Long大神给我的类,把用来运行IronRuby脚本的代码封装了下,用起来挺方便.我也是现学现卖,有问题还请大家指出TM这代码高亮搞毛啊
[查看全文]
本帖最后由 轻舟过 于 2012-8-1 00:55 编辑
最近两天做了Haskell 99题中的前20道
函数式编程解决问题的思路还真是不一样
还有函数式编程是不是研究Lisp更好一点。。
题目链接在这里:http://www.haskell.org/haskellwiki/99_questions
相应的还有Prolog、Lisp、Perl、OCaml的99题
论坛的SyntaxHighlighter插件怎么没有Haskell的语法高亮
[mw_shl_code=text,true]-- #1
myLast :: [a] -> a
myLast (x:[]) = x
myLast (x:xs) = myLast xs
-- #2
myButLast :: [a] -> a
myButLast (x1:x2:[]) = x1
myButLast (x1:x2:xs) = myButLast (x2:xs)
-- #3
elementAt :: [a] -> Int -> a
elementAt (x:xs) 1 = x
elementAt (x:xs) idx = elementAt xs (idx - 1)
-- #4
myLength :: [a] -> Int
myLength xs = foldl (+) 0 $ map (\x -> 1) xs
-- #5
myReverse :: [a] -> [a]
myReverse = foldl (\xs x-> x:xs) []
-- #6
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome xs = xs == myReverse xs
data NestedList a = Elem a | List [NestedList a] deriving (Show)
-- #7
flatten :: NestedList a -> [a]
flatten (Elem x) = [x]
flatten (List lst) = foldl (++) [] $ map flatten lst
-- #8
compress :: (Eq a) => [a] -> [a]
compress [] = []
compress all@(x:xs) = (head $ takeWhile (==x) all):compress(dropWhile (==x) all)
-- #9
pack :: (Eq a) => [a] -> [[a]]
pack [] = []
pack all@(x:xs) = (takeWhile (==x) all):pack(dropWhile (==x) all)
-- #10
encode :: (Eq a) => [a] -> [(Int, a)]
encode [] = []
encode all@(x:xs) = ((myLength $ takeWhile (==x) all), x):encode(dropWhile (==x) all)
data RunLengthCode a = Single a | Multiple Int a deriving (Show)
-- #11
encodeModified :: (Eq a) => [a] -> [RunLengthCode a]
encodeModified [] = []
encodeModified lst = map toRunLengthCode $ encode lst
where toRunLengthCode pair = if fst pair == 1 then (Single (snd pair)) else (Multiple (fst pair) (snd pair))
-- #12
decodeModified :: [RunLengthCode a] -> [a]
decodeModified [] = []
decodeModified ((Single x):xs) = [x] ++ decodeModified(xs)
decodeModified ((Multiple len x):xs) = (replicate len x) ++ decodeModified(xs)
-- #13
encodeDirect :: (Eq a) => [a] -> [RunLengthCode a]
encodeDirect [] = []
encodeDirect all@(x:xs) = code:encodeDirect(dropWhile (==x) all)
where len = myLength $ takeWhile (==x) all
code = if len == 1 then (Single x) else (Multiple len x)
-- #14
dupli :: [a] -> [a]
dupli [] = []
dupli (x:xs) = [x, x] ++ dupli(xs)
-- #15
repli :: [a] -> Int -> [a]
repli [] _ = []
repli (x:xs) cnt = (replicate cnt x) ++ (repli xs cnt)
-- #16
{-
- dropEvery = flip $ \n -> map snd . filter ((n/=) . fst) . zip (cycle [1..n])
-}
dropEvery :: [a] -> Int -> [a]
dropEvery lst cnt = map snd $ filter (\p -> (fst p) `mod` cnt /= 0) $ zip [1..] lst
-- #17
split :: [a] -> Int -> ([a], [a])
split lst len = (take len lst, drop len lst)
-- #18
slice :: [a] -> Int -> Int -> [a]
slice lst start end = fst $ split (drop (start - 1) lst) (end - start + 1)
-- #19
rotate :: [a] -> Int -> [a]
rotate lst num = (drop len lst) ++ (take len lst)
where lstLen = length lst
len = (num `mod` lstLen + lstLen) `mod` lstLen
-- #20
removeAt :: Int -> [a] -> (a, [a])
removeAt num lst = (head lstTail, (take (num - 1) lst) ++ (tail lstTail))
where lstTail = drop (num - 1) lst
[/mw_shl_code]
然后还有spoj上的TEST
[mw_shl_code=text,true]main = do
line <- getLine
if line == "42"
then getContents
else do
putStrLn line
main[/mw_shl_code]
[查看全文]
本帖最后由 Richeir 于 2012-8-2 09:42 编辑
`6`
这次就辛苦 @Richeir @轻舟过 @前原圭一 @秋声赋 4个僵尸轮流写帖子了~
时间是整个8月份,由@Richeir 开始,轮流写,每人一周至少写一个帖子,这样1个月下来,12个帖子基本足够讲解一个编程语言的基础了。
| 7.29 | 【Ruby】0.你好Ruby | https://www.gn00.com/t-88274-1-1.html |
| 8.2 | 【Ruby】1.1 标准Ruby环境的部署 | https://www.gn00.com/t-90723-1-1.html |