在Haskell中调用嵌套函数 [英] Invoking nested functions in Haskell
问题描述
我在一个名为 person
的函数内定义了一个名为 initials
的函数,但我无法弄清楚在 person
之外呼叫首字母
:
main =
- 我试图在这里打印initials函数的输出。
(putStrLn((personfirstNamelastName)。initials)) - 不在范围内:`首字母'
- 如果此函数调用正常工作,则输出为f.l.。
person firstName lastName =
firstName ++ [。] ++ lastName
其中
fullName = firstName ++++ lastName
firstInitial = firstName !! 0
lastInitial =姓氏! 0
initials = [firstInitial] ++。 ++ [lastInitial] ++。
是否可以调用首字母
函数在 main
函数中,即使它是在 person
函数中定义的
您误解了其中
的作用:它只是定义了定义,这意味着它们只能用于在这种情况下,首字母
只能用于 person
。
换句话说,您正在定义一个函数 person :: String - >字符串 - > [String]
(取两个字符串并返回它们的列表),其中此函数的内部实现细节包含一些名称为 initials
。它不像其他语言中的结构或类,其中首字母
是某些值,所有 person
s可以访问。 (无论如何 person
只是返回一个字符串列表,而不是一个新类型。)另外,。
不是一个字段访问器,它只是一个方便的帮助函数组合。
看起来你可能想要制作一个 Person
数据类型,类似于
data Person = Person {firstName :: String,lastName :: String}
<现在,您可以编写一个函数,它接受
Person
并返回它们的首字母: - 使用模式匹配
首字母(Person {firstName = f,lastName = l})=
[f! 0] ++。 ++ [l !! 0] ++。
- 或者,使用通过记录语法隐式定义的访问器
initials p = [firstName p !! 0] ++。 ++ [lastName p !! 0] ++。
这个用法就像
putStrLn(首字母(Person {firstName =first,lastName =last}))
- 或者只是使用位置参数(它仍然适用于记录语法)
putStrLn(首字母(Personfirstlast))
I have defined a function called initials
inside a function called person
, but I can't figure out how to call initials
outside of person
:
main =
--I attempted to print the output of the initials function here.
(putStrLn ((person "firstName" "lastName") . initials)) --Not in scope: `initials'
--If this function call worked correctly, the output would be "f.l.".
person firstName lastName =
firstName ++ ["."] ++ lastName
where
fullName = firstName ++ " " ++ lastName
firstInitial = firstName !! 0
lastInitial = lastName !! 0
initials = [firstInitial] ++ "." ++ [lastInitial] ++ "."
Is it possible to invoke the initials
function inside the main
function, even though it is defined inside the person
function?
You're misunderstanding what where
does: it just scopes definitions, meaning they can only be used in the item they are attached to, in this case, initials
can only be used in person
.
In other words, you are defining a function person :: String -> String -> [String]
(taking two strings and returning a list of them), where the internal implementation details of this function include some (unused) value called initials
. It is not like a struct or class in other languages, where initials
is some value that all person
s can access. (And anyway person
is just returning a list of strings, not a new type.) Also, .
is not a field accessor, it is just a convenient helper for function composition.
It seems like you might be wanting to make a Person
data type, something like
data Person = Person { firstName :: String, lastName :: String }
This is defining Person
via "record syntax".
Now, you can write a function that takes a Person
and returns their initials:
-- using pattern matching
initials (Person {firstName = f, lastName = l}) =
[f !! 0] ++ "." ++ [l !! 0] ++ "."
-- or, using the accessors implicitly defined via record syntax
initials p = [firstName p !! 0] ++ "." ++ [lastName p !! 0] ++ "."
This is used like
putStrLn (initials (Person { firstName = "first", lastName = "last" }))
-- or, just using positional arguments (which still work with record syntax)
putStrLn (initials (Person "first" "last"))
这篇关于在Haskell中调用嵌套函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!