在表达式中让..使用警卫 [英] Using guards in let .. in expressions

查看:70
本文介绍了在表达式中让..使用警卫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我写这样的代码

solveLogic :: Int -> Int -> Int
solveLogic a b =
    let 
        x = 1
        brainiac
            | a >= x     = 1
            | a == b     = 333
            | otherwise  = 5
    in
        brainiac

每次我敦促编写不需要不必要的"brainiac"功能的东西时,就像这样:

And every time I have urge to write this things without unneeded "brainiac" function, like this:

solveLogic :: Int -> Int -> Int
solveLogic a b =
    let 
        x = 1
    in
        | a >= x     = 1
        | a == b     = 333
        | otherwise  = 5

哪个代码更像是"Haskellish".有什么办法吗?

Which code is much more "Haskellish". Is there any way of doing this?

推荐答案

是的,使用where子句:

solveLogic a b
        | a >= x     = 1
        | a == b     = 333
        | otherwise  = 5
    where
      x = 1

这篇关于在表达式中让..使用警卫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆