首先考虑以下代码,它计算字符串中的空格数:
countSpaces :: String -> Int
countSpaces [] = 0
countSpaces (c : restOfString)
= d + countSpaces restOfString
where d = if c == ’ ’ then 1
else 0
Run Code Online (Sandbox Code Playgroud)
这是我的问题:
1,如果我拨打电话:countSpaces "abc"
,那么当这个函数试图匹配字符串会发生什么"abc"
用(c: restOfString)
.我的意思restOfString
是:"abc"
在这次通话中,但是这是(c:restOfString)
什么?你正在"勉强"某事(变量?c
)restOfstring
然后你想要匹配?我只是不明白.
2,我尝试运行代码但是我得到一个解析错误为什么?
输入'''上的解析错误
3,这个函数会调用countSpaces
无穷大吗?因为restOfString
总是相同而不是减少,例如.countSpaces "aaa"
第一次通话,第二次或任何通话后拨打电话都不会改变,对吧?
现在考虑完成相同任务的代码:
countSpaces :: String -> Int
countSpaces [] = 0
countSpaces (’ ’ : restOfString)
= 1 + …
Run Code Online (Sandbox Code Playgroud) 我知道Haskell具有参数化的数据类型:
data Maybe a = Nothing | Just a
Run Code Online (Sandbox Code Playgroud)
但有没有办法限制a
可以表示的类型?特别是,我想创建一个类型
data Tag a = Tag a
Run Code Online (Sandbox Code Playgroud)
这样a
可以采取两种类型TagPrimitive
或类型TagComplex
(但是,我不希望它是可能的,a
是的类型,比方说,Integer
或String
什么的,让我的程序没有意义).
这可能吗?
看看这个ZeroMQ HelloWorldClient.hs代码片段:
forM_ [1..10] $ \i -> do
liftIO . putStrLn $ "Sending Hello " ++ show i ++ "…"
send requester [] "Hello"
_ <- receive requester
liftIO . putStrLn $ "Received World " ++ show i
Run Code Online (Sandbox Code Playgroud)
有什么理由_ <- receive requester
不写成receive requester
?
另外,一般来说,有没有理由使用_ <- f
(where f : Monad m => m a
)而不是f
?
这是一个简短的例子.我想知道为什么在TypeClass示例中我没有明确说明,forall
而在函数定义中没有forall
它无法编译:
Couldn't match kind ‘Nat’ with ‘*’
When matching types
proxy0 :: Nat -> *
Proxy :: * -> *
Expected type: proxy0 n0
Actual type: Proxy p0
In the first argument of ‘natVal’, namely ‘(Proxy :: Proxy p)’
In the second argument of ‘($)’, namely ‘natVal (Proxy :: Proxy p)’
In the first argument of ‘(++)’, namely
‘(show $ natVal (Proxy :: Proxy p))’
Run Code Online (Sandbox Code Playgroud)
码:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE …
Run Code Online (Sandbox Code Playgroud) 我正在阅读这篇关于用Scotty在Haskell中编写API的博客文章,我看到了关于monad变换器的部分.我理解monad变换器的概念,但我无法理解这里发生的事情:
let r m = runReaderT (runConfigM m) c
Run Code Online (Sandbox Code Playgroud)
如何表达的参考m
,当m
在同一声明let
使用它体现在哪里?这里发生了什么?什么是m
?
看看Haskell Prelude中的定义getLine
,我得到了递归的工作方式,在你点击换行符之前你一直要求一个字符,然后你构建一个列表然后返回包含在IO中.
但是我的问题是这些return
语句在这种情况下是如何工作的,特别是return (c:....:return "")
在你遇到基本情况时如何工作.你如何return ""
进入清单?
我想要获得monad的支持.我认为与国家monad一起玩是个不错的做法.
使用CLaSH的UART Rx :
-- UART RX Logic
data RxReg
= RxReg
{ _rx_reg :: BitVector 8
, _rx_data :: BitVector 8
, _rx_sample_cnt :: Unsigned 4
, _rx_cnt :: Unsigned 4
, _rx_frame_err :: Bool
, _rx_over_run :: Bool
, _rx_empty :: Bool
, _rx_d1 :: Bit
, _rx_d2 :: Bit
, _rx_busy :: Bool
}
makeLenses ''RxReg
uartRX r@(RxReg {..}) rx_in uld_rx_data rx_enable = flip execState r $ do
-- Synchronise the async signal
rx_d1 .= …
Run Code Online (Sandbox Code Playgroud) 我知道C编译器能够获取独立的代码,并针对它们所针对的特定系统从中生成独立的shellcode。
例如,在中给出以下内容anon.c
:
int give3() {
return 3;
}
Run Code Online (Sandbox Code Playgroud)
我可以跑
Run Code Online (Sandbox Code Playgroud)gcc anon.c -o anon.obj -c objdump -D anon.obj
这给了我(在MinGW上):
anon1.obj: file format pe-i386
Disassembly of section .text:
00000000 <_give3>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: b8 03 00 00 00 mov $0x3,%eax
8: 5d pop %ebp
9: c3 ret
a: 90 nop
b: 90 nop
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样制作main:
main.c
#include <stdio.h>
#include <stdint.h>
int main(int argc, char **argv)
{
uint8_t shellcode[] = {
0x55,
0x89, 0xe5,
0xb8, …
Run Code Online (Sandbox Code Playgroud) 我已经编写了这个函数map
,但是我需要使用list comprehension来编写它:
alter = map (\x -> if x == 0 then 1 else 0)
Run Code Online (Sandbox Code Playgroud)
它给出了例如
alter [1,1,0]
> [0,0,1]
Run Code Online (Sandbox Code Playgroud) 我试图打破行下使用\n
,putStrLn
并print
但没有任何工程.
当我使用\n
结果时只连接字符串,当我使用putStrLn
或print
我收到类型错误.
输出\n
:
formatLines [("a",12),("b",13),("c",14)]
"a...............12\nb...............13\nc...............14\n"
Run Code Online (Sandbox Code Playgroud)
输出putStrLn
:
format.hs:6:22:
Couldn't match type `IO ()' with `[Char]'
Expected type: String
Actual type: IO ()
In the return type of a call of `putStrLn'
In the expression:
putStrLn (formatLine ((fst x), (snd x)) ++ formatLines xs)
In an equation for `formatLines':
formatLines (x : xs)
= putStrLn (formatLine ((fst x), (snd x)) ++ formatLines xs)
Failed, modules …
Run Code Online (Sandbox Code Playgroud) 我学习LYAH,并从第1章的最后一个问题是要找到a
,b
并c
在那里(a,b,c)
形成一个直角三角形,它们的总和为24 a
,b
,c
都是Int
秒.
所以我有下面的代码(本书不一样).
Prelude> [(a,b,c)|c<-[1..10],b<-[1..10],a<-[1..10],a^2+b^2==c^2,a+b+c==24]
[(8,6,10),(6,8,10)]
Run Code Online (Sandbox Code Playgroud)
我希望能得到所有可能的组合
[(8,6,10),(6,8,10),(8,10,6),(6,10,8),(10,6,8),(10,8,6)]
Run Code Online (Sandbox Code Playgroud)
为什么只显示部分内容?我尝试了不同版本的ghci,但结果相同.谢谢.
doubleMe :: Int -> Int
doubleMe x = x + x
doubleUs :: Int -> Int
doubleUs x y = doubleMe x+y
Run Code Online (Sandbox Code Playgroud)
我试图利用该功能doubleMe
,以获得双倍的总和x
及y
.我究竟做错了什么?
不编译的新代码:
doubleMe :: Int -> Int
doubleMe x = x + x
doubleUs :: Int -> Int -> Int
doubleUs x y = x*2 + y*2
doubleUs2 :: Int -> Int -> Int
doubleUs2 :: x y = doubleMe (x+y)
Run Code Online (Sandbox Code Playgroud)