我睡不着!:)
我在Haskell编写了一个构建双链表的小程序.基本语言的属性是懒惰的评估(参见下面的一堆代码).我的问题是,我可以做在同一个纯粹的功能性语言急于评估或不?在任何情况下,渴望功能语言必须具备哪些属性才能构建这样的结构(杂质?)?
import Data.List
data DLList a = DLNull |
DLNode { prev :: DLList a
, x :: a
, next :: DLList a
}
deriving (Show)
walkDLList :: (DLList a -> DLList a) -> DLList a -> [a]
walkDLList _ DLNull = []
walkDLList f n@(DLNode _ x _) = x : walkDLList f (f n)
-- Returns first and last items.
makeDLList :: [a] -> (DLList a, DLList a)
makeDLList xs …Run Code Online (Sandbox Code Playgroud) 我有这两个功能:
primes = sieve [2..]
where
sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0]
isPrime number = number /= 1 && null [x | x <- takeWhile (\x -> x < (ceiling . sqrt) number) primes, mod number x == 0]
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试加载包含这些函数的模块时,我看到以下错误消息:
[2 of 2] Compiling Main ( euler37.hs, interpreted )
euler37.hs:6:70:
No instance for (RealFrac Int)
arising from a use of `ceiling'
Possible fix: add an instance declaration for (RealFrac Int)
In the …Run Code Online (Sandbox Code Playgroud)