gpt4 book ai didi

haskell - let..in 中 where 子句的范围

转载 作者:行者123 更新时间:2023-12-02 18:08:00 24 4
gpt4 key购买 nike

encode :: String -> String
encode xs =
let s = normalize xs
in case s of
[] -> []
_ -> encode' s row col
where row = isqrt (length s)
col = length s `div` row

这会给出错误“变量不在范围内:s​​”。看来 let..in 中的 where 子句无法看到之前定义的变量 s

我该如何重写这个?

最佳答案

where 是在函数级别定义的,由于 s 没有在该级别定义,因此无法访问 s。您可以在 let ... in ... 级别的头部添加额外的声明:

encode :: String -> String
encode xs =
let s = normalize xs
<strong>row = isqrt (length s)</strong>
<strong>col = length s `div` row</strong>
in case s of
[] -> []
_ -> encode' s row col

或者将所有内容移至 where 子句:

encode :: String -> String
encode xs = case s of
[] -> []
_ -> encode' s row col
where s = normalize xs
ns = length s
<strong>row = isqrt ns</strong>
<strong>col = ns `div` row</strong>

关于haskell - let..in 中 where 子句的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72887869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com