我有一个声明的列表:
lst = [-1,6,4,13]
并且我想过滤值大于0且小于10的列表元素。我写道:
filterList :: [Int] -> [Int]
filterList lst = filter (>0 && <10) lst
当我运行:t过滤器时,我可以看到:
filter (a -> Bool) -> [a] -> [a]
但是当我运行编译器时,显示的错误是由于filter子句中的“ <10”。这不是我指定要过滤的值大于零且小于10的正确方法吗?
答案 0 :(得分:4)
filterList lst =过滤器(> 0 && <10)lst
问题在于,这不是Haskell中的有效表达式。但是,即使我们添加括号来消除解析错误:
filterList lst = filter ((>0) && (<10)) lst -- no parse errors, but types are not matched
我们将收到错误消息,指出类型不匹配,这是正确的,因为&&
期望两个参数都为Bool
,但是(> 0)
和(< 10)
是功能Integer -> Bool
。
函数似乎缺少参数,但是我们可以提供一些参数:
filterList lst = filter (\x -> (x > 0) && (x < 10)) lst
答案 1 :(得分:2)
您可以使用列表推导。
private void uploadFile() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
Uri audioUri = audioFileUri.fromFile(new File(savepath)); // URI for audio file
Uri fx = audioFileUri.fromFile(new File(fxpath)); // URI for audio effects
if (audioFileUri != null) {
StorageReference imageReference = storageRef.child("images").child(System.currentTimeMillis() + "");
StorageReference audioRef = storageRef.child("audio").child(System.currentTimeMillis() + ".3pg"); // storage location to firebase.
StorageReference fxRef = storageRef.child("effects").child(fxpath + ".mp3"); // storage location to firebase
// Upload for attach effects audio file
fxRef.putFile(fx).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Audio Effect Uploaded! ", Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred())/ taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + " % done");
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Audio Effect Failed! ", Toast.LENGTH_LONG).show();
}
});
// Upload attach audio file
audioRef.putFile(audioUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Audio Uploaded!", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Upload for Image
imageReference.putFile(audioFileUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
//and displaying error message
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploading Image File"+(int)progress+"%");
}
});
} else {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "No File Selected.", Toast.LENGTH_LONG).show();
}
}
或者两次内置的过滤器功能。
[x | x <- lst, x<10, x>0]