小编Cac*_*tus的帖子

与列表模式比较

首先考虑以下代码,它计算字符串中的空格数:

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 pattern-matching

0
推荐指数
1
解决办法
144
查看次数

限制类型参数在数据声明中可以采用的类型

我知道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是的类型,比方说,IntegerString什么的,让我的程序没有意义).

这可能吗?

haskell types singleton-type

0
推荐指数
1
解决办法
61
查看次数

使用(_ < - f)代替f单独?

看看这个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

haskell do-notation

0
推荐指数
1
解决办法
64
查看次数

类型类方法与独立函数中的范围类型变量

这是一个简短的例子.我想知道为什么在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)

haskell

0
推荐指数
1
解决办法
118
查看次数

这个let表达式发生了什么?

我正在阅读这篇关于用Scotty在Haskell中编写API的博客文章,我看到了关于monad变换器的部分.我理解monad变换器的概念,但我无法理解这里发生的事情:

let r m = runReaderT (runConfigM m) c
Run Code Online (Sandbox Code Playgroud)

如何表达的参考m,当m在同一声明let使用它体现在哪里?这里发生了什么?什么是m

syntax haskell let

0
推荐指数
1
解决办法
82
查看次数

getLine如何在haskell中工作?

看看Haskell Prelude中的定义getLine,我得到了递归的工作方式,在你点击换行符之前你一直要求一个字符,然后你构建一个列表然后返回包含在IO中.

但是我的问题是这些return语句在这种情况下是如何工作的,特别是return (c:....:return "")在你遇到基本情况时如何工作.你如何return ""进入清单?

io haskell functional-programming getline

0
推荐指数
1
解决办法
652
查看次数

如何使用State monad重构代码以通过添加更多函数来增加模块性?

我想要获得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)

monads haskell record clash

0
推荐指数
1
解决办法
67
查看次数

为匿名函数创建C语言附加组件是否可行?

我知道C编译器能够获取独立的代码,并针对它们所针对的特定系统从中生成独立的shellcode。

例如,在中给出以下内容anon.c

int give3() {
    return 3;
}
Run Code Online (Sandbox Code Playgroud)

我可以跑

gcc anon.c -o anon.obj -c
objdump -D anon.obj
Run Code Online (Sandbox Code Playgroud)

这给了我(在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)

c assembly lambda functional-programming shellcode

0
推荐指数
1
解决办法
181
查看次数

如何在haskell中使用列表理解来编写函数?

我已经编写了这个函数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)

haskell list-comprehension pointfree

0
推荐指数
1
解决办法
269
查看次数

我如何在Haskell上打破界限?

我试图打破行下使用\n,putStrLnprint但没有任何工程.

当我使用\n结果时只连接字符串,当我使用putStrLnprint我收到类型错误.

输出\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)

haskell

0
推荐指数
1
解决办法
914
查看次数

Haskell的子集操作并没有给我所有结果,只显示了部分结果?

我学习LYAH,并从第1章的最后一个问题是要找到a,bc在那里(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,但结果相同.谢谢.

haskell list-comprehension filter

-2
推荐指数
1
解决办法
33
查看次数

功能顺序应用程序

doubleMe :: Int -> Int
doubleMe x = x + x

doubleUs :: Int -> Int
doubleUs x y = doubleMe x+y
Run Code Online (Sandbox Code Playgroud)

我试图利用该功能doubleMe,以获得双倍的总和xy.我究竟做错了什么?

不编译的新代码:

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)

haskell

-4
推荐指数
2
解决办法
104
查看次数