Haskell
杨过悔
这个作者很懒,什么都没留下…
展开
-
操作缓存文件,熟悉File操作
-- file: ch07/tempfile.hsimport System.IOimport System.Directory(getTemporaryDirectory, removeFile)import Control.Exception(finally)main :: IO ()main = withTempFile "mytemp.txt" myAction{- The转载 2015-07-07 21:55:27 · 406 阅读 · 0 评论 -
用Haskell写出高效的List
再次阅读haskell体会fromhttp://learnyouahaskell.com/for-a-few-monads-more newtype DiffList a = DiffList{ getDiffList :: [a] -> [a] }toDiffList :: [a] -> DiffList atoDiffList xs = DiffList (原创 2016-03-19 23:16:06 · 824 阅读 · 0 评论 -
神奇的客串化
do 代码块实际上是把操作连接在一起的快捷记号。有两个运算符可以用来代替 do 代码块: >> 和 >>= 。在 ghci 看一下它们的类型:ghci> :type (>>)(>>) :: (Monad m) => m a -> m b -> m bghci> :type (>>=)(>>=) :: (Monad m) => m a -> (a -> m b) -> m转载 2015-07-07 23:35:08 · 358 阅读 · 0 评论 -
sublime text开发haskell
我们常用WINGHCI来编写haskell,不过不是很方便我们使用sublime来开发haskell以及sublime text直接运行,sublime text目前自带且已经支持直接运行好几门语言了,尤其是FP语言编写后直接按 CTRL + B直接运行原创 2015-07-07 22:48:28 · 1689 阅读 · 0 评论 -
return
在haskell中return是与其他语言不一样的,return是Monad过程,结果是Monad,所以我们可以从return中取出结果即反Monad过程Prelude> let a = return 1Prelude> a1Prelude> :type aa :: (Num a, Monad m) => m aPrelude> b <- aPrelude> b1Prelude原创 2015-07-07 23:45:24 · 385 阅读 · 0 评论 -
How to cabal install
cabal is a command-line program for downloading and building software written inHaskell.It can install all kinds of fascinating and useful software packages from theHackage repository.It is excell转载 2015-07-14 08:05:32 · 1345 阅读 · 0 评论 -
序列化与反序列化
以字符串的方式序列化需要序列化的变量*Main> let v = (True,1,2)*Main> v写入文件*Main> writeFile "v.txt" (show v)读取文件*Main> input 还原类型*Main> (read input)::(Bool,Int,Int)(True,1,2)*Main> :type vv ::原创 2015-07-07 10:37:45 · 343 阅读 · 0 评论 -
定义类型类
data Color = Red|Blue|Greenclass BasicEq a where isEqual :: a->a->Bool isEqual x y = not (isNotEqual x y) isNotEqual :: a->a->Bool isNotEqual x y = not (isEqual x y)instance BasicEq Bool whe原创 2015-07-07 10:17:17 · 540 阅读 · 0 评论 -
自动派生
对于一些简单的数据类型可以让haskell帮我们自动实例化,这里说的是类型类的派生例如data Color = Red | Green | Blue deriving (Read,Show,Eq,Ord)我们就可不必自已实现Color类型的Read,Show,Eq,Ord的类型类如instance Show Color where show Red = "it is Red原创 2015-07-07 13:05:10 · 511 阅读 · 0 评论 -
一个Hashell模块
1.暴露模块的接口module ModuleName (XXX) whereXXX为暴露的内容2.导入模块import XXXXXX为模块名3.代码来之Real World Haskell定义JsonSimpleJSON.hsmodule SimpleJSON ( JValue(..) , ge原创 2015-07-06 20:38:04 · 622 阅读 · 0 评论 -
实现折叠语法
fold1 :: (a->b->a) ->a ->[b] -> afold1 step zero (x:xs) = fold1 step (step zero x) xsfold1 _ zero [] = zerofoldSum xs = fold1 step 0 xs where step acc x = acc + x学了点Haskell就能很好很好的理解什么是FP"过程"了,所转载 2015-07-06 10:19:29 · 691 阅读 · 0 评论 -
Adler-32算法
Adler-32 是一个简单的校验和算法,但这个 JAVA 实现还是非常复杂,很难看清楚位操作之间的关系。以下是 Adler-32 算法的 Haskell 实现:-- file: ch04/Adler32.hsimport Data.Char (ord)import Data.Bits (shiftL, (.&.), (.|.))base = 65521adl转载 2015-07-06 09:56:50 · 1844 阅读 · 0 评论 -
使用尾递归实现String To Int
import Data.Char (digitToInt)asInt xs = loop 0 xsloop :: Int -> String->Intloop acc [] = accloop acc (x:xs) = let acc' = acc * 10 + digitToInt x in loop acc' xs这里要认识一个概念:结构递归与非结构递归有处理空类原创 2015-07-06 09:12:40 · 376 阅读 · 0 评论 -
实现自已的Map
import Data.Char (toUpper)square2 :: [Int] -> [Int]square2 xs = map lambdaSq xs where lambdaSq x = x*xupperCase2 xs = map toUpper xsmap2 :: (a->b)->[a]->[b]map2 f (x:xs) = f x:map2 f xsmap2原创 2015-07-06 09:33:42 · 325 阅读 · 0 评论 -
组合类型
hashell固然是提供了函数组合的操作如module Main whereimport Data.Char(toUpper)module Main wheremain = do interact (map toUpper . (++) "Your data, in uppercase, is:\n\n")。为组合符号其中(++) "Your data, in up原创 2015-07-07 23:04:54 · 482 阅读 · 0 评论 -
Action
之前学scala时有个不解的问题,现在在haskell里找到答案了-- file: ch07/actions.hsstr2action :: String -> IO ()str2action input = putStrLn ("Data: " ++ input)list2actions :: [String] -> [IO ()]list2actions = map st转载 2015-07-07 23:28:44 · 372 阅读 · 0 评论 -
haskell 安装gtk 适合win7GUI编程
由于版本和库依赖问题所以花费了很多时间去解决,所以必须记录下来 1.下载gtk+-bundle_3.4.2-20130513_win32(把名字贴在google上就找到了,记着必须用这个版本,否则可能出现与gtk(haskell)兼容问题)安装后把目录下的/bin加入path路径中(因为我们需要使用其中的dll编译haskell版的gtk库) 2.下载Haske原创 2016-03-22 21:42:25 · 954 阅读 · 0 评论