小编Jos*_*ica的帖子

乘/加双倍返回错误

我编写了这个程序来将摄氏度转换为华氏度,但该程序只返回不正确的数字。由于我对 C 语言或编程很陌生,我不知道应该尝试解决什么问题。

#include <stdio.h>
#include <stdlib.h>

int main(void){

        double a;
        double b = a * 1.8;
        double c = b + 32;

        printf("Enter a temp in celcius");
        scanf("%lf", &a);
        printf("%f", &c);



        return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

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

无需显式递归即可计算 f 0 + f 1 + … + fn 的 Haskell 函数

我设计了一个函数“total” total :: (Integer -> Integer) -> (Integer -> Integer),使得总 f 是在值为 n 时给出总 f 0 + f 1 + … + fn 的函数,它使用内置的 Haskell 函数而不是显式递归。

现在,我的第一直觉是使用 map 函数将所述函数应用于整数列表 [1..n],然后使用 sum 函数来计算它,但它在我身上出错并给出以下消息:

code.hs:8:17: error:
    • Couldn't match type ‘[b0]’ with ‘[Integer] -> Integer’
      Expected type: (a0 -> b0)
                     -> (Integer -> Integer) -> [Integer] -> Integer
        Actual type: (a0 -> b0) -> [a0] -> [b0]
    • In the first argument of ‘sum’, namely ‘map’
      In the expression: sum map …
Run Code Online (Sandbox Code Playgroud)

haskell

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

无法理解为什么可能在 Haskell 中键入签名

嗨,我在理解函数所需的类型签名时遇到了麻烦。

-- findPassword :: Map.Map Hash Passwd -> Int -> Hash -> Maybe Passwd
findPassword rTable width hashVal = do
    let usefulHashes = take (width+1) (iterate (pwHash.pwReduce) hashVal)
    let hashesInMap = [i | i <- usefulHashes, Map.member i rTable]
    let goodPass = [ rTable Map.! j | j <- hashesInMap]
    let findPass = listToMaybe [ helper k hashVal width | k <- goodPass, (helper k hashVal width) /= "" ] 
    return findPass
    where
        helper :: Passwd -> Hash -> Int …
Run Code Online (Sandbox Code Playgroud)

haskell types maybe type-signature

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

为什么 Rust 不推断 never 类型?

考虑这个 Rust 代码:

fn loop_forever() {
    loop {
        
    }
}

fn main() {
    let hello = if true { "Hello, world!" } else { loop_forever() };
    println!("{}", hello);
}
Run Code Online (Sandbox Code Playgroud)

的返回类型loop_forever被推断为(),因此编译失败,因为它与 的类型不兼容"Hello, world!"。但返回类型可以是!相反的,如果我编写fn loop_forever() -> ! {而不是让它被推断,那么它就可以正常工作。那么为什么 Rust 不首先推断出这一点呢?

type-inference rust bottom-type

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

遵循 Haskell 讲座但代码无法正常工作

我正在使用 Haskell 进行讲座并编写了一个简单的函数

getNth :: Int -> [a] -> a
getNth n (x:xs) = if n == 0 then x else getNth (n-1) xs
Run Code Online (Sandbox Code Playgroud)

但是,我收到错误

getNth :: Int -> [a] -> a
getNth n (x:xs) = if n == 0 then x else getNth (n-1) xs
Run Code Online (Sandbox Code Playgroud)

我写的和我教授在讲座上写的一模一样,我错过了什么吗?

当我尝试在 GHCi 中运行时,它也不起作用

haskell

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

标签 统计

haskell ×3

bottom-type ×1

c ×1

maybe ×1

rust ×1

type-inference ×1

type-signature ×1

types ×1