foo:: Int -> Int -> Int
foo z x = if (z < 100)
then z * foo (z+(x*z)) z
else z
Run Code Online (Sandbox Code Playgroud)
每次从它自己调用时,你如何打印出(整数z)一个输出?你能有返回IO和Int的函数吗?你需要辅助功能吗?
我想为 FPGA 构建自己的最小 RISC-V 处理器。处理器将尽可能简单,只有一条管道。
我阅读了整个 RISC-V ISA,其中有很多标准扩展。那么可以运行linux的最小RISC-V ISA是多少呢?
这对于检查Haskell中的多个语句有效吗?或者这有更好的方法吗?
case ((x > -10) && (x < 20),x /= 9,(x `mod` 2) == 0,x) of
(False,_,_,_) -> error "Not in range"
(_,False,_,_) -> error "Must not be 9"
(_,_,False,_) -> error "Must be even"
(True,True,True,10) -> stuff ()
(True,True,True,20) -> stuff ()
_ -> error "Error Message"
Run Code Online (Sandbox Code Playgroud) 既然Haskell的数据是不可变的,那么你如何在全局存储一个可以被任何函数修改的列表?你可以将它读取并将其存储在循环中吗?还是将列表写入文件?我需要记录用户点击的按钮数量.
checkstring :: [String] -> Int -> [String]
checkstring p n = do z <- doesFileExist (p !! n)
if z
then p
else error $ "'" ++ (p !! n) ++ "' file path does not exist"
Run Code Online (Sandbox Code Playgroud)
它通过查看"n"检查字符串中的元素(因此,如果n = 2,它将检查列表中的第二个字符串),然后查看它是否存在.如果它确实存在,它将返回原始字符串列表,如果不存在则会出错.为什么这样做?:
Couldn't match expected type `[t0]' with actual type `IO Bool'
In the return type of a call of `doesFileExist'
In a stmt of a 'do' expression: z <- doesFileExist (p !! n)
Run Code Online (Sandbox Code Playgroud) 我有两个不等长的列表.当我添加它们时,我希望最终列表具有最长列表的长度.
addtwolists [0,0,221,2121] [0,0,0,99,323,99,32,2332,23,23]
>[0,0,221,2220,323,99,32,2332,23,23]
addtwolists [945,45,4,45,22,34,2] [0,34,2,34,2]
>[945,79,6,79,24,34,2]
zerolist :: Int -> [Integer]
zerolist x = take x (repeat 0)
addtwolists :: [Integer] -> [Integer] -> [Integer]
addtwolists x y = zipWith (+) (x ++ (zerolist ((length y)-(length x)))) (y ++ (zerolist ((length x)-(length y))))
Run Code Online (Sandbox Code Playgroud)
这段代码效率低下.所以我尝试过:
addtwolist :: [Integer] -> [Integer] -> [Integer]
addtwolist x y = zipWith (+) (x ++ [head (zerolist ((length y)-(length x))) | (length y) > (length x)]) (y ++ [head (zerolist ((length x)-(length y))) …Run Code Online (Sandbox Code Playgroud) 我仍然不知道字节串是如何工作的
import qualified Data.ByteString.Lazy as BS
let x = BS.readFile "somefile.txt" --some large file
let z = ((reverse (BS.unpack x)) !! 2) --do stuff here
Run Code Online (Sandbox Code Playgroud)
我知道bytestrings可以用来非常快速有效地读取大量数据.但打开包装没有意义.
let z = readArray x 1 --can you read the bytestring like its a array?(something like this)
Run Code Online (Sandbox Code Playgroud)
你不能只是在没有解压缩的情况下以字节串形式读取数据吗?或者只是解压缩一段数据?
你能解释一下它是如何工作的吗?(代码示例)