我写了一个有签名的函数
sort :: [Int] -> [Int]
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误
模糊的发生'排序'
我知道已经有一个名为sort in的内置函数
import Data.List
如何在保持相同类型签名的同时解决此问题?
我在Haskell中编写一个代码,它取一个0和1的列表,如[1,1,0,0,1,0,1]返回列表中0和1的数字出现的对(元组)(如( 3,4).
这是我的代码:
inc :: Int -> Int
inc x = (\x -> x + 1) x
count :: [Int] -> (Int,Int)
c = (0,0)
count x =
if null x
then c
else if head x == 0
then do
inc (fst c)
count (tail x)
else if head x == 1
then do
inc (snd c)
count (tail x)
Run Code Online (Sandbox Code Playgroud)
我也尝试过这种保护形式:
count :: [Int] -> (Int,Int)
c = (0,0)
count x
| null x = c
| head x …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)