Haskell 函数参数不在函数体的范围内?

Haskell 函数参数不在函数体的范围内?,haskell,Haskell,我正在通过一些练习学习Haskell(为了更好) 以下是我对泡泡运动的尝试: bubbleSort :: [Integer] -> [Integer] bubbleSort xs = let (state, bubbled) = bubble True xs in if not state then bubbleSort bubbled

我正在通过一些练习学习Haskell(为了更好)

以下是我对泡泡运动的尝试:

bubbleSort :: [Integer] -> [Integer]
bubbleSort xs = let 
                  (state, bubbled) = bubble True xs
                in  
                  if not state
                  then bubbleSort bubbled
                  else bubbled
  where
    bubble :: Bool -> [Integer] -> (Bool, [Integer])
    bubble changed [] = (changed, []) 
    bubble changed [x] = (changed, [a])
    bubble changed (a:b:as) | compare a b == GT = (fst bubble False (a:as), b:(snd bubble False (a:as)))
                            | otherwise = (fst bubble (changed && True) (b:as), a:(snd bubble (changed && True) (b:as))) 
此错误出现在最后一行(在otherwise子句中):
不在范围内:“a”

老实说,我不确定我是很累还是错过了一些非常基本的东西,但是,据我所知,a应该在范围内,因为它是作为(a:b:as)模式的一部分传入的?
这不正确吗?

您的
否则
案例很好,但不是这个案例:
气泡改变了[x]=(改变了[a])
<代码>a不在范围内

import           Data.List       (sort)
import           Test.QuickCheck

main :: IO ()
main = verboseCheck isValidSort >> verboseCheck idempotent

isValidSort, idempotent :: [Integer] -> Bool
isValidSort xs = sort xs == bubbleSort xs
idempotent  xs = bubbleSort (bubbleSort xs) == bubbleSort xs

bubbleSort :: [Integer] -> [Integer]
bubbleSort xs = let (state, bubbled) = bubble True xs
                in if not state
                   then bubbleSort bubbled
                   else bubbled
  where
    bubble :: Bool -> [Integer] -> (Bool, [Integer])
    bubble changed [] = (changed, [])
    bubble changed [x] = (changed, [x])
    bubble changed (a:b:as) | compare a b == GT = (fst $ bubble False (a:as), b:(snd $ bubble False (a:as)))
                            | otherwise = (fst $ bubble (changed && True) (b:as), a:(snd $ bubble (changed && True) (b:as)))

您的
否则
案例很好,但不是这个案例:
气泡已更改[x]=(已更改[a])
<代码>a不在范围内

import           Data.List       (sort)
import           Test.QuickCheck

main :: IO ()
main = verboseCheck isValidSort >> verboseCheck idempotent

isValidSort, idempotent :: [Integer] -> Bool
isValidSort xs = sort xs == bubbleSort xs
idempotent  xs = bubbleSort (bubbleSort xs) == bubbleSort xs

bubbleSort :: [Integer] -> [Integer]
bubbleSort xs = let (state, bubbled) = bubble True xs
                in if not state
                   then bubbleSort bubbled
                   else bubbled
  where
    bubble :: Bool -> [Integer] -> (Bool, [Integer])
    bubble changed [] = (changed, [])
    bubble changed [x] = (changed, [x])
    bubble changed (a:b:as) | compare a b == GT = (fst $ bubble False (a:as), b:(snd $ bubble False (a:as)))
                            | otherwise = (fst $ bubble (changed && True) (b:as), a:(snd $ bubble (changed && True) (b:as)))

您的
否则
案例很好,但不是这个案例:
气泡已更改[x]=(已更改[a])
<代码>a不在范围内

import           Data.List       (sort)
import           Test.QuickCheck

main :: IO ()
main = verboseCheck isValidSort >> verboseCheck idempotent

isValidSort, idempotent :: [Integer] -> Bool
isValidSort xs = sort xs == bubbleSort xs
idempotent  xs = bubbleSort (bubbleSort xs) == bubbleSort xs

bubbleSort :: [Integer] -> [Integer]
bubbleSort xs = let (state, bubbled) = bubble True xs
                in if not state
                   then bubbleSort bubbled
                   else bubbled
  where
    bubble :: Bool -> [Integer] -> (Bool, [Integer])
    bubble changed [] = (changed, [])
    bubble changed [x] = (changed, [x])
    bubble changed (a:b:as) | compare a b == GT = (fst $ bubble False (a:as), b:(snd $ bubble False (a:as)))
                            | otherwise = (fst $ bubble (changed && True) (b:as), a:(snd $ bubble (changed && True) (b:as)))

您的
否则
案例很好,但不是这个案例:
气泡已更改[x]=(已更改[a])
<代码>a不在范围内

import           Data.List       (sort)
import           Test.QuickCheck

main :: IO ()
main = verboseCheck isValidSort >> verboseCheck idempotent

isValidSort, idempotent :: [Integer] -> Bool
isValidSort xs = sort xs == bubbleSort xs
idempotent  xs = bubbleSort (bubbleSort xs) == bubbleSort xs

bubbleSort :: [Integer] -> [Integer]
bubbleSort xs = let (state, bubbled) = bubble True xs
                in if not state
                   then bubbleSort bubbled
                   else bubbled
  where
    bubble :: Bool -> [Integer] -> (Bool, [Integer])
    bubble changed [] = (changed, [])
    bubble changed [x] = (changed, [x])
    bubble changed (a:b:as) | compare a b == GT = (fst $ bubble False (a:as), b:(snd $ bubble False (a:as)))
                            | otherwise = (fst $ bubble (changed && True) (b:as), a:(snd $ bubble (changed && True) (b:as)))


我认为问题是,
fst bubble False(a:as)
需要是
fst$bubble False(a:as)
,并且同样调用w/where
snd
。这就是错误所在:
bubble changed[x]=(changed[a])
。不在
否则
子句中。我认为问题是
fst bubble False(a:as)
需要
fst$bubble False(a:as)
并且同样调用w/where
snd
。这就是错误所在:
bubble changed[x]=(changed[a])
。不在
否则
子句中。我认为问题是
fst bubble False(a:as)
需要
fst$bubble False(a:as)
并且同样调用w/where
snd
。这就是错误所在:
bubble changed[x]=(changed[a])
。不在
否则
子句中。我认为问题是
fst bubble False(a:as)
需要
fst$bubble False(a:as)
并且同样调用w/where
snd
。这就是错误所在:
bubble changed[x]=(changed[a])
。不在
否则
子句中。虽然这很有效,而且非常棒,但老实说,我无法说出我的代码有什么区别,以及为什么我的代码不起作用:/@阿布拉罕。调用
fst
函数时出现了一个小问题
fst bubble False(a:as)
试图将
fst
应用于
bubble
,这是一个类型错误,您首先需要使用
($)
计算等式的右侧。因此,执行fst$bubble False(a:as)可以实现这一点。
a
不在范围内,因为它是在不同的情况下定义的。你的
否则
案例很好,但不是这个案例:
泡泡改变了[x]=(改变了[a])
虽然这很有效,但这很可怕,我真的不知道我的代码有什么不同,为什么我的代码不起作用:/@阿布拉罕。调用
fst
函数时出现了一个小问题
fst bubble False(a:as)
试图将
fst
应用于
bubble
,这是一个类型错误,您首先需要使用
($)
计算等式的右侧。因此,执行fst$bubble False(a:as)可以实现这一点。
a
不在范围内,因为它是在不同的情况下定义的。你的
否则
案例很好,但不是这个案例:
泡泡改变了[x]=(改变了[a])
虽然这很有效,但这很可怕,我真的不知道我的代码有什么不同,为什么我的代码不起作用:/@阿布拉罕。调用
fst
函数时出现了一个小问题
fst bubble False(a:as)
试图将
fst
应用于
bubble
,这是一个类型错误,您首先需要使用
($)
计算等式的右侧。因此,执行fst$bubble False(a:as)可以实现这一点。
a
不在范围内,因为它是在不同的情况下定义的。你的
否则
案例很好,但不是这个案例:
泡泡改变了[x]=(改变了[a])
虽然这很有效,但这很可怕,我真的不知道我的代码有什么不同,为什么我的代码不起作用:/@阿布拉罕。调用
fst
函数时出现了一个小问题
fst bubble False(a:as)
试图将
fst
应用于
bubble
,这是一个类型错误,您首先需要使用
($)
计算等式的右侧。因此,执行fst$bubble False(a:as)可以实现这一点。
a
不在范围内,因为它是在不同的情况下定义的。您的
否则
案例很好,但不是这个案例:
气泡已更改[x]=(已更改[a])