我是一个学习C#的初学者,我正在制作一个模拟购物清单收据程序来管理您的购物。我生成 .txt 收据,但是右对齐字符串有问题,这是我的代码;
public static void GenerateNewReciept(Customer customer)
{
List<ShoppingItems> customerPurchaedItems;
try
{
sw = File.AppendText("receipt.txt");
//Customer object
Customer customers = customer;
//List of all items in list
List<ShoppingList> customerItems = customer.CustomerShoppingList;
DateTime todayDandT = DateTime.Now;
//Making reciept layout
sw.WriteLine("Date Generated: " + todayDandT.ToString("g", CultureInfo.CreateSpecificCulture("en-us")));
sw.WriteLine("Customer: " + customer.FName + " " + customer.SName1);
for (int i = 0; i < customerItems.Count; i++)
{
customerPurchaedItems = customerItems[i].ShoppingItems;
foreach (ShoppingItems item in customerPurchaedItems)
{
sw.WriteLine(String.Format("{0,0} {1,25:C2}", item.ItemName, item.Price));
}
}
sw.WriteLine("Total …Run Code Online (Sandbox Code Playgroud) 我已设法实现模拟过滤器功能(经过多次尝试)
filter' :: (Num a, Eq a) => (a -> Bool) -> [a] -> [a]
filter' a [] = []
filter' a (x:xs) = if a x
then x : filter' a xs
else filter' a xs
Run Code Online (Sandbox Code Playgroud)
我不清楚的是类型声明
filter' :: (Num a, Eq a) => (a -> Bool) -> [a] -> [a]
-- filter' (<10) [1,2,3]
-- output = []
Run Code Online (Sandbox Code Playgroud)
我们传入(<10) [1,2,3].但是在类型声明中,(a -> Bool)我们传递一个来自列表的递归,输出为true或false.然而,表达测试(<10)?怎么样我们为什么不添加另一个Bool?
我有一个函数addIndex [1,2,3] = [(0,1),(1,2),(2,3)]应该为每一对添加一个索引我在实现它时遇到了麻烦.这就是我所做的;
addIndexs :: [Int] -> [(Int,Int)]
addIndexs [] = []
addIndexs x = zip x [1..length x]
Run Code Online (Sandbox Code Playgroud)
我能够使用zip put实现它我感觉它的作弊,我希望能够在不使用任何预构建函数的情况下实现它.
我没有使用预构建功能的最接近的是
addindex x = [(i,z) | z <- x, i <-[1..length x]]
Run Code Online (Sandbox Code Playgroud)
但是这会为每个元组添加所有索引
addIndex [1,2,3] = [(0,1),(1,1),(2,1),(0,2),(1,2),(2,2)...]
Run Code Online (Sandbox Code Playgroud)
我只是个初学者.
我正在努力学习haskell我坚持以下问题
记下一个函数split :: RawText - > [Word],它将给定的字符串拆分为单词列表.
为了简单起见,我们假设原始文本只是一串字母和空格,可能有不规则的间距.例如:split"one two three ff"= ["one","two","three","ff"]
我一直得到["一个"***例外:recursion.hs:30:1-14:函数中的非详尽模式溢出'所以递归永远不会结束,但我有一个基本情况.
我的功能
type RawText = String
-- [Char] 'a','b','c','d','e'
type SingleWord = String
type Line = [SingleWord]
type Page = [Line]
-- [-------CHAR-----]
-- split " one two three ff " = ["one","two","three","ff"]
-- [char],[char],[char],[char]
-- " " is delimiter for a []
split' :: RawText -> [SingleWord]
spilt' [] = []
split' xs
| xs == [] = []
| isBlank (head xs) = split' xs
| …Run Code Online (Sandbox Code Playgroud)